Contains.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (comparer == null)
  18. throw new ArgumentNullException(nameof(comparer));
  19. return Contains(source, value, comparer, CancellationToken.None);
  20. }
  21. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. return Contains(source, value, CancellationToken.None);
  26. }
  27. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (comparer == null)
  32. throw new ArgumentNullException(nameof(comparer));
  33. return source.Any(x => comparer.Equals(x, value), cancellationToken);
  34. }
  35. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. return source.Contains(value, EqualityComparer<TSource>.Default, cancellationToken);
  40. }
  41. }
  42. }