IgnoreElements.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Ignores all elements in an async-enumerable sequence leaving only the termination messages.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <returns>An empty async-enumerable sequence that signals termination, successful or exceptional, of the source sequence.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> IgnoreElements<TSource>(this IAsyncEnumerable<TSource> source)
  19. {
  20. if (source == null)
  21. throw Error.ArgumentNull(nameof(source));
  22. return Core(source);
  23. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  24. {
  25. await foreach (var _ in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  26. {
  27. }
  28. yield break;
  29. }
  30. }
  31. }
  32. }