Advertisement
Intermediate C#, Asynchronous Programming

What are async streams in C#, and how does IAsyncEnumerable<T> differ from a regular IEnumerable<T> for handling large or slow data sources?

Short answer

A regular IEnumerable assumes all its elements are already available (or cheaply computable) synchronously; an async stream, defined via IAsyncEnumerable combined with yield return inside an async method, produces elements one at a time asynchronously, letting each element be…

Save C# mock interview
Advertisement
AModel answer
A regular IEnumerable<T> assumes all its elements are already available (or cheaply computable) synchronously; an async stream, defined via IAsyncEnumerable<T> combined with yield return inside an async method, produces elements one at a time asynchronously, letting each element be awaited individually as it becomes available -- consumed via 'await foreach', this lets an application process data that arrives gradually (a paginated API, a large file read line by line, a real-time feed) without blocking and without loading the entire dataset into memory upfront.
async IAsyncEnumerable<int> GenerateNumbersAsync() {
    for (int i = 1; i <= 5; i++) {
        await Task.Delay(1000); // simulating a slow data source
        yield return i;
    }
}

await foreach (var number in GenerateNumbersAsync()) {
    Console.WriteLine(number); // processes each value as it arrives, without blocking
}
Real-world example
A service consuming a paginated third-party API wraps the page-fetching logic in an async stream, letting calling code iterate over all records via 'await foreach' as if it were a simple in-memory collection, while pages are actually being fetched lazily and asynchronously behind the scenes.
Likely follow-ups
  • How does await foreach differ from a plain foreach in terms of what happens between iterations?
  • What's the memory advantage of IAsyncEnumerable<T> over loading an entire large dataset into a List<T> first?
Related topics