Contains.cs 5.0 KB

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