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