IAsyncEnumerator.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Threading;
  5. using System.Threading.Tasks;
  6. namespace System.Collections.Generic
  7. {
  8. /// <summary>
  9. /// Asynchronous version of the IEnumerator&lt;T&gt; interface, allowing elements to be
  10. /// retrieved asynchronously.
  11. /// </summary>
  12. /// <typeparam name="T">Element type.</typeparam>
  13. public interface IAsyncEnumerator<out T> : IDisposable
  14. {
  15. /// <summary>
  16. /// Gets the current element in the iteration.
  17. /// </summary>
  18. T Current { get; }
  19. /// <summary>
  20. /// Advances the enumerator to the next element in the sequence, returning the result asynchronously.
  21. /// </summary>
  22. /// <returns>
  23. /// Task containing the result of the operation: true if the enumerator was successfully advanced
  24. /// to the next element; false if the enumerator has passed the end of the sequence.
  25. /// </returns>
  26. Task<bool> MoveNextAsync();
  27. }
  28. /// <summary>
  29. /// Provides a set of extension methods for <see cref="IAsyncEnumerator{T}"/>.
  30. /// </summary>
  31. public static class AsyncEnumerator
  32. {
  33. /// <summary>
  34. /// Advances the enumerator to the next element in the sequence, returning the result asynchronously.
  35. /// </summary>
  36. /// <param name="source">The enumerator to advance.</param>
  37. /// <param name="cancellationToken">Cancellation token that can be used to cancel the operation.</param>
  38. /// <returns>
  39. /// Task containing the result of the operation: true if the enumerator was successfully advanced
  40. /// to the next element; false if the enumerator has passed the end of the sequence.
  41. /// </returns>
  42. public static Task<bool> MoveNextAsync<T>(this IAsyncEnumerator<T> source, CancellationToken cancellationToken)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. cancellationToken.ThrowIfCancellationRequested();
  47. return source.MoveNextAsync();
  48. }
  49. }
  50. }