↑ IAsyncEnumerable<T>
interface exposes an enumerator that provides asynchronous iteration over values of a specified type.
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
}
public interface IAsyncEnumerator<out T> : IAsyncDisposable
{
ValueTask<bool> MoveNextAsync();
T Current { get; }
}
public interface IAsyncDisposable
{
ValueTask DisposeAsync();
}
await foreach (var number in GetNumbersAsync())
{
Console.WriteLine(number);
}
static async IAsyncEnumerable<int> GetNumbersAsync()
{
for (int i = 1; i <= 5; i++)
{
await Task.Delay(1000);
yield return i;
}
}
// Output:
// 1
// 2
// 3
// 4
// 5