0
0
C Sharp (C#)programming~30 mins

Stream vs async stream behavior in C Sharp (C#) - Hands-On Comparison

Choose your learning style9 modes available
Stream vs Async Stream Behavior
📖 Scenario: You are working on a program that reads numbers from a list and processes them. You want to understand the difference between reading data using a normal stream (synchronous) and an async stream (asynchronous).
🎯 Goal: Build a simple C# program that first reads numbers from a list using a normal IEnumerable<int> stream, then reads numbers asynchronously using an IAsyncEnumerable<int> stream. You will see how the async stream allows waiting for data asynchronously.
📋 What You'll Learn
Create a list of integers called numbers with values 1, 2, 3, 4, 5.
Create a synchronous method GetNumbers() that returns IEnumerable<int> from the numbers list.
Create an asynchronous method GetNumbersAsync() that returns IAsyncEnumerable<int> and yields numbers with a 500ms delay between each.
Use a foreach loop to print numbers from GetNumbers().
Use an await foreach loop to print numbers from GetNumbersAsync().
Print messages to show when synchronous and asynchronous reading starts and ends.
💡 Why This Matters
🌍 Real World
Reading data from files, databases, or network sources often requires asynchronous streams to avoid blocking the program while waiting for data.
💼 Career
Understanding synchronous vs asynchronous streams is important for writing efficient and responsive C# applications, especially in web development and data processing.
Progress0 / 4 steps
1
Create the list of numbers
Create a list of integers called numbers with the exact values 1, 2, 3, 4, 5.
C Sharp (C#)
Need a hint?

Use List<int> and initialize it with { 1, 2, 3, 4, 5 }.

2
Create synchronous and asynchronous methods
Add a synchronous method GetNumbers() that returns IEnumerable<int> from the numbers list. Also add an asynchronous method GetNumbersAsync() that returns IAsyncEnumerable<int> and yields each number with a 500ms delay using await Task.Delay(500).
C Sharp (C#)
Need a hint?

Use yield return to return each number in GetNumbers(). In GetNumbersAsync(), add await Task.Delay(500) before yielding each number.

3
Print numbers synchronously and asynchronously
In the Main method, first print "Synchronous reading:" then use a foreach loop with variables number to iterate over GetNumbers() and print each number. Then print "Asynchronous reading:" and use an async Task Main() method with await foreach loop over GetNumbersAsync() to print each number.
C Sharp (C#)
Need a hint?

Use Console.WriteLine to print messages. Use foreach for synchronous and await foreach for asynchronous iteration.

4
Run and observe output
Run the program and observe the output. It should first print numbers 1 to 5 immediately under "Synchronous reading:" and then print numbers 1 to 5 with a delay under "Asynchronous reading:".
C Sharp (C#)
Need a hint?

Run the program and check that the synchronous numbers print immediately, then the asynchronous numbers print with delay.