Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Async Streams and IAsyncEnumerable | Async Loops and Processing Collections
C# Async and Await Practice

bookAsync Streams and IAsyncEnumerable

Async streams in C# offer a powerful way to process sequences of data as they become available, rather than waiting for an entire collection to be ready. This is especially useful when dealing with data sources where items arrive over time, such as reading lines from a file, fetching records from a database, or consuming messages from an API.

An async stream is represented by the IAsyncEnumerable<T> interface, and you can iterate through it using the await foreach statement. With async streams, you can write iterator methods that use yield return in combination with await, making it possible to asynchronously produce and consume data in a non-blocking manner.

Note
Definition

IAsyncEnumerable<T> is an interface representing an asynchronous stream of data, where each element can be awaited as it becomes available.
await foreach is a C# statement used to asynchronously iterate over an IAsyncEnumerable<T>, allowing you to process each item as soon as it arrives without blocking the thread.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { await foreach (var number in GenerateNumbersAsync()) { Console.WriteLine($"Received number: {number}"); } } public static async IAsyncEnumerable<int> GenerateNumbersAsync() { for (int i = 1; i <= 5; i++) { await Task.Delay(500); // Simulate asynchronous work yield return i; } } } }

Unlike regular collections such as List<T> or arrays, async streams do not require all items to be available at once. Instead, each item is produced and consumed asynchronously. This makes async streams ideal for scenarios where data arrives over time, or when processing large data sets that should not be loaded into memory all at once. You use the await foreach statement to consume items from an IAsyncEnumerable<T>, allowing your code to remain responsive and efficient while waiting for new data.

AsyncStreamWithCancellation.cs

AsyncStreamWithCancellation.cs

copy
12345678910111213141516171819202122232425262728293031323334
// This is a non-runnable code sample demonstrating cancellation with async streams. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp { public class AsyncStreamWithCancellation { // Async iterator method supporting cancellation public static async IAsyncEnumerable<int> GetNumbersAsync( [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken) { for (int i = 1; i <= 10; i++) { cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(1000, cancellationToken); // Simulate async work with cancellation yield return i; } } // Consuming the async stream with cancellation public static async Task ConsumeNumbersAsync(CancellationToken cancellationToken) { await foreach (var number in GetNumbersAsync(cancellationToken), cancellationToken) { Console.WriteLine(number); } } } }

Async streams really shine in situations where you need to process data as it becomes available rather than waiting for a complete data set. For instance, when reading a very large file, you can yield each line as soon as it is read, allowing your application to start processing lines immediately and keep memory usage low. Similarly, when streaming data from APIs, you can handle each message or data chunk as soon as it arrives, improving responsiveness and scalability. This approach is also valuable in real-time scenarios, such as monitoring logs or handling user events, where waiting for all data up front is not practical.

Key scenarios for async streams:

  • Reading large files line by line;
  • Streaming data from remote APIs or web sockets;
  • Processing real-time events, such as logs or user actions;
  • Handling data pipelines where items arrive gradually.

1. What is the main benefit of using IAsyncEnumerable?

2. How do you consume an async stream in C#?

3. In what scenarios are async streams especially useful?

question mark

What is the main benefit of using IAsyncEnumerable?

Select the correct answer

question mark

How do you consume an async stream in C#?

Select the correct answer

question mark

In what scenarios are async streams especially useful?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookAsync Streams and IAsyncEnumerable

Scorri per mostrare il menu

Async streams in C# offer a powerful way to process sequences of data as they become available, rather than waiting for an entire collection to be ready. This is especially useful when dealing with data sources where items arrive over time, such as reading lines from a file, fetching records from a database, or consuming messages from an API.

An async stream is represented by the IAsyncEnumerable<T> interface, and you can iterate through it using the await foreach statement. With async streams, you can write iterator methods that use yield return in combination with await, making it possible to asynchronously produce and consume data in a non-blocking manner.

Note
Definition

IAsyncEnumerable<T> is an interface representing an asynchronous stream of data, where each element can be awaited as it becomes available.
await foreach is a C# statement used to asynchronously iterate over an IAsyncEnumerable<T>, allowing you to process each item as soon as it arrives without blocking the thread.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace ConsoleApp { public class Program { public static async Task Main(string[] args) { await foreach (var number in GenerateNumbersAsync()) { Console.WriteLine($"Received number: {number}"); } } public static async IAsyncEnumerable<int> GenerateNumbersAsync() { for (int i = 1; i <= 5; i++) { await Task.Delay(500); // Simulate asynchronous work yield return i; } } } }

Unlike regular collections such as List<T> or arrays, async streams do not require all items to be available at once. Instead, each item is produced and consumed asynchronously. This makes async streams ideal for scenarios where data arrives over time, or when processing large data sets that should not be loaded into memory all at once. You use the await foreach statement to consume items from an IAsyncEnumerable<T>, allowing your code to remain responsive and efficient while waiting for new data.

AsyncStreamWithCancellation.cs

AsyncStreamWithCancellation.cs

copy
12345678910111213141516171819202122232425262728293031323334
// This is a non-runnable code sample demonstrating cancellation with async streams. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp { public class AsyncStreamWithCancellation { // Async iterator method supporting cancellation public static async IAsyncEnumerable<int> GetNumbersAsync( [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken) { for (int i = 1; i <= 10; i++) { cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(1000, cancellationToken); // Simulate async work with cancellation yield return i; } } // Consuming the async stream with cancellation public static async Task ConsumeNumbersAsync(CancellationToken cancellationToken) { await foreach (var number in GetNumbersAsync(cancellationToken), cancellationToken) { Console.WriteLine(number); } } } }

Async streams really shine in situations where you need to process data as it becomes available rather than waiting for a complete data set. For instance, when reading a very large file, you can yield each line as soon as it is read, allowing your application to start processing lines immediately and keep memory usage low. Similarly, when streaming data from APIs, you can handle each message or data chunk as soon as it arrives, improving responsiveness and scalability. This approach is also valuable in real-time scenarios, such as monitoring logs or handling user events, where waiting for all data up front is not practical.

Key scenarios for async streams:

  • Reading large files line by line;
  • Streaming data from remote APIs or web sockets;
  • Processing real-time events, such as logs or user actions;
  • Handling data pipelines where items arrive gradually.

1. What is the main benefit of using IAsyncEnumerable?

2. How do you consume an async stream in C#?

3. In what scenarios are async streams especially useful?

question mark

What is the main benefit of using IAsyncEnumerable?

Select the correct answer

question mark

How do you consume an async stream in C#?

Select the correct answer

question mark

In what scenarios are async streams especially useful?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
some-alt