Skip to content

Latest commit

 

History

History
54 lines (44 loc) · 1.08 KB

iasyncenumerable.md

File metadata and controls

54 lines (44 loc) · 1.08 KB

IAsyncEnumerable<T>

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();
}

Example

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

Links

↑ Iterating with Async Enumerables in C# 8.