Challenge - 5 Problems
Async Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of async stream enumeration
What is the output of this C# code that uses an async stream?
C Sharp (C#)
using System; using System.Collections.Generic; using System.Threading.Tasks; class Program { static async IAsyncEnumerable<int> GetNumbersAsync() { for (int i = 1; i <= 3; i++) { await Task.Delay(10); yield return i; } } static async Task Main() { await foreach (var num in GetNumbersAsync()) { Console.Write(num); } } }
Attempts:
2 left
💡 Hint
Remember that await foreach prints each number as it is yielded without spaces.
✗ Incorrect
The async stream yields numbers 1, 2, and 3 sequentially with a small delay. The Console.Write prints them without spaces, so the output is '123'.
🧠 Conceptual
intermediate1:30remaining
Understanding IAsyncEnumerable behavior
Which statement about IAsyncEnumerable in C# is true?
Attempts:
2 left
💡 Hint
Think about how async streams help with data that arrives over time.
✗ Incorrect
IAsyncEnumerable supports asynchronous iteration using await foreach, allowing processing of data as it becomes available without blocking.
🔧 Debug
advanced2:30remaining
Identify the runtime error in async stream code
What runtime error will this code produce when executed?
C Sharp (C#)
using System; using System.Collections.Generic; using System.Threading.Tasks; class Program { static async IAsyncEnumerable<int> GenerateAsync() { for (int i = 0; i < 3; i++) { if (i == 1) throw new InvalidOperationException("Error at 1"); yield return i; } } static async Task Main() { try { await foreach (var n in GenerateAsync()) { Console.Write(n); } } catch (Exception ex) { Console.Write(ex.GetType().Name); } } }
Attempts:
2 left
💡 Hint
Look at where the exception is thrown inside the async stream.
✗ Incorrect
The exception is thrown during the second iteration (i == 1), so the catch block prints the exception type name 'InvalidOperationException'.
📝 Syntax
advanced2:00remaining
Which code snippet correctly defines an async stream?
Select the code snippet that correctly defines an async stream method returning IAsyncEnumerable.
Attempts:
2 left
💡 Hint
An async stream method must be async and use yield return with await inside.
✗ Incorrect
Option A correctly declares an async method returning IAsyncEnumerable and uses await with yield return.
🚀 Application
expert2:30remaining
How many items are produced by this async stream?
Given this async stream method, how many items will be produced when enumerated?
C Sharp (C#)
using System.Collections.Generic; using System.Threading.Tasks; async IAsyncEnumerable<int> ProduceItems() { int count = 0; while (count < 5) { await Task.Delay(5); if (count % 2 == 0) yield return count; count++; } }
Attempts:
2 left
💡 Hint
Count how many numbers from 0 to 4 are even.
✗ Incorrect
The method yields only even numbers less than 5: 0, 2, and 4, so 3 items total.