CodeBlanks Logo Icon CodeBlanks

IAsyncEnumerable

C# course · Advanced · Lesson 19/83

Learn async iteration with IAsyncEnumerable<T>.

What is IAsyncEnumerable?

IAsyncEnumerable<T> is a collection that produces values asynchronously as a stream. Each element is produced with an await; this lets you process large data in pieces. Instead of awaiting a Task<List<T>> for the whole list, you can stream each element one by one.

A method returns async IAsyncEnumerable<T>. Its body uses yield return to produce values. The consumer uses await foreach to await each element. This is especially suited for database and API streams.

Defining a stream

An IAsyncEnumerable<T> method is marked async and produces values with yield return. Example: static async IAsyncEnumerable<int> Numbers() { await Task.CompletedTask; yield return 1; yield return 2; }.

Each yield return produces an element and pauses the stream; the consumer resumes when ready. You can use await at each step; this is used to wait for input/output in real async scenarios.

Exercise Define and consume an IAsyncEnumerable

Define static async IAsyncEnumerable<int> Numbers(). In its body write await Task.CompletedTask;, yield return 1;, yield return 2;, yield return 3;. Make Main a static async Task Main. Sum with int sum = 0; await foreach (int n in Numbers()) { sum += n; } and print with Console.WriteLine(sum);.

Show example solution
In Numbers body write await Task.CompletedTask; yield return 1; yield return 2; yield return 3;. Make Main static async Task Main() and write int sum = 0; await foreach (int n in Numbers()) { sum += n; } Console.WriteLine(sum);
Example solution:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program
{
    static async IAsyncEnumerable<int> Numbers()
    {
        await Task.CompletedTask;
        yield return 1;
        yield return 2;
        yield return 3;
    }

    static async Task Main()
    {
        int sum = 0;
        await foreach (int n in Numbers())
        {
            sum += n;
        }
        Console.WriteLine(sum);
    }
}

Test yourself

  1. What does IAsyncEnumerable do?

    • It awaits a Task
    • It produces a stream of values asynchronously
    • It returns a List<T>
    • It starts a thread
    Show answer

    It produces a stream of values asynchronously — Correct. IAsyncEnumerable produces a stream of values asynchronously.

  2. Complete the IAsyncEnumerable declaration

    static ____ IAsyncEnumerable<int> Numbers() { await Task.CompletedTask; yield return 1; }
    Show answer
    static async IAsyncEnumerable<int> Numbers() { await Task.CompletedTask; yield return 1; }
  3. Why use IAsyncEnumerable?

    • To await the whole list at once
    • To write synchronous code
    • To process large data in pieces asynchronously
    • To manage the thread pool
    Show answer

    To process large data in pieces asynchronously — Correct. IAsyncEnumerable is for processing large data in pieces asynchronously.

All lessons in Advanced