Contains.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. #if REFERENCE_ASSEMBLY
  10. public static partial class AsyncEnumerableDeprecated
  11. #else
  12. public static partial class AsyncEnumerable
  13. #endif
  14. {
  15. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  16. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.containsasync?view=net-9.0-pp
  17. //
  18. // These two overloads are replaced by the single method above, which takes a comparer, but supplies a default
  19. // value of null.
  20. /// <summary>
  21. /// Determines whether an async-enumerable sequence contains a specified element by using the default equality comparer.
  22. /// </summary>
  23. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  24. /// <param name="source">An async-enumerable sequence in which to locate a value.</param>
  25. /// <param name="value">The value to locate in the source sequence.</param>
  26. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  27. /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence contains an element that has the specified value.</returns>
  28. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  29. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  30. public static ValueTask<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken = default) =>
  31. source is ICollection<TSource> collection ? new ValueTask<bool>(collection.Contains(value)) :
  32. ContainsAsync(source, value, comparer: null, cancellationToken);
  33. /// <summary>
  34. /// Determines whether an async-enumerable sequence contains a specified element by using a specified System.Collections.Generic.IEqualityComparer{T}.
  35. /// </summary>
  36. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  37. /// <param name="source">An async-enumerable sequence in which to locate a value.</param>
  38. /// <param name="value">The value to locate in the source sequence.</param>
  39. /// <param name="comparer">An equality comparer to compare elements.</param>
  40. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  41. /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence contains an element that has the specified value.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="comparer"/> is null.</exception>
  43. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  44. public static ValueTask<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
  45. {
  46. if (source == null)
  47. throw Error.ArgumentNull(nameof(source));
  48. //
  49. // See https://github.com/dotnet/corefx/pull/25097 for the optimization here.
  50. //
  51. if (comparer == null)
  52. {
  53. return Core(source, value, cancellationToken);
  54. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  55. {
  56. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  57. {
  58. if (EqualityComparer<TSource>.Default.Equals(item, value))
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. }
  66. else
  67. {
  68. return Core(source, value, comparer, cancellationToken);
  69. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  70. {
  71. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  72. {
  73. if (comparer.Equals(item, value))
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. }
  81. }
  82. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  83. }
  84. }