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?
IntermediateAnswer
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.