Contains.cs 1.9 KB

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