Contains.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (comparer == null)
  18. {
  19. throw new ArgumentNullException(nameof(comparer));
  20. }
  21. return Contains(source, value, comparer, CancellationToken.None);
  22. }
  23. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value)
  24. {
  25. if (source == null)
  26. {
  27. throw new ArgumentNullException(nameof(source));
  28. }
  29. return Contains(source, value, CancellationToken.None);
  30. }
  31. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. {
  35. throw new ArgumentNullException(nameof(source));
  36. }
  37. if (comparer == null)
  38. {
  39. throw new ArgumentNullException(nameof(comparer));
  40. }
  41. return source.Any(x => comparer.Equals(x, value), cancellationToken);
  42. }
  43. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  44. {
  45. if (source == null)
  46. {
  47. throw new ArgumentNullException(nameof(source));
  48. }
  49. return source.Contains(value, EqualityComparer<TSource>.Default, cancellationToken);
  50. }
  51. }
  52. }