Skip to content

C# Async Streams — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# Async Streams. We cover key concepts, practical examples, and best practices.

You need to stream data from a paginated API or a slow producer. Loading everything into memory is wasteful. IAsyncEnumerable<T> lets you await each item as it arrives, producing and consuming data asynchronously without blocking or buffering everything upfront.

Wrong

async Task<List<int>> GetResultsAsync()
{
    var results = new List<int>();
    for (int i = 0; i < 100; i++)
    {
        results.Add(await FetchSlowlyAsync(i)); // Buffers all results
    }
    return results;
}

Output: All 100 items are loaded into memory before the caller can process the first one.

async IAsyncEnumerable<int> GetResultsAsync()
{
    for (int i = 0; i < 100; i++)
    {
        yield return await FetchSlowlyAsync(i);
    }
}

// Consumer
await foreach (var item in GetResultsAsync())
{
    Console.WriteLine(item); // Processes each item as it arrives
}

Output: Items are processed one at a time. Memory stays constant regardless of total items.

Async streams work with cancellation tokens, LINQ, and System.Threading.Channels:

await foreach (var item in GetResultsAsync().WithCancellation(ct))
{
    Process(item);
}

Prevention

  • Use IAsyncEnumerable<T> for paginated API calls, streaming data, and log processing.
  • Use await foreach to consume async streams.
  • Use ConfigureAwait(false) on the async stream: await foreach (var x in stream.ConfigureAwait(false)).
  • Use WithCancellation(ct) to support cancellation in async streams.
  • Use System.Threading.Channels to bridge producers and consumers across threads.
  • Use LINQ extension methods for async streams: SelectAwait, WhereAwait, ToListAsync.

Common Mistakes with async stream

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What is the difference between IEnumerable and IAsyncEnumerable?

IEnumerable<T> is synchronous — the producer blocks until the next item is ready. IAsyncEnumerable<T> is asynchronous — the producer awaits between items, freeing the thread. Use IAsyncEnumerable when each item requires async I/O (HTTP calls, database queries, file reads).

Can I use LINQ with IAsyncEnumerable?

Yes. The System.Linq.Async package (from dotnet/reactive) provides LINQ operators for async streams: WhereAwait, SelectAwait, ToListAsync, ToArrayAsync, etc.

How do I convert a Task> to IAsyncEnumerable

Use ToAsyncEnumerable() from the System.Linq.Async package, or yield each item: foreach (var item in await GetListAsync()) yield return item;.

Async streams power Doda Browser's extension update checker — streaming manifest downloads without buffering. For more C# async patterns, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro