Contains.cs 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. /// <summary>
  12. /// Determines whether an async-enumerable sequence contains a specified element by using the default equality comparer.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence in which to locate a value.</param>
  16. /// <param name="value">The value to locate in the source sequence.</param>
  17. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  18. /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence contains an element that has the specified value.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  20. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  21. public static ValueTask<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken = default) =>
  22. source is ICollection<TSource> collection ? new ValueTask<bool>(collection.Contains(value)) :
  23. ContainsAsync(source, value, comparer: null, cancellationToken);
  24. /// <summary>
  25. /// Determines whether an async-enumerable sequence contains a specified element by using a specified System.Collections.Generic.IEqualityComparer{T}.
  26. /// </summary>
  27. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  28. /// <param name="source">An async-enumerable sequence in which to locate a value.</param>
  29. /// <param name="value">The value to locate in the source sequence.</param>
  30. /// <param name="comparer">An equality comparer to compare elements.</param>
  31. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  32. /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence contains an element that has the specified value.</returns>
  33. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="comparer"/> is null.</exception>
  34. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  35. public static ValueTask<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. //
  40. // See https://github.com/dotnet/corefx/pull/25097 for the optimization here.
  41. //
  42. if (comparer == null)
  43. {
  44. return Core(source, value, cancellationToken);
  45. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  46. {
  47. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  48. {
  49. if (EqualityComparer<TSource>.Default.Equals(item, value))
  50. {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. }
  57. else
  58. {
  59. return Core(source, value, comparer, cancellationToken);
  60. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  61. {
  62. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  63. {
  64. if (comparer.Equals(item, value))
  65. {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. }
  72. }
  73. }
  74. }