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

Async streams with IAsyncEnumerable in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
        }
    }
}
A123
BCompilation error
C3 2 1
D1 2 3
Attempts:
2 left
💡 Hint
Remember that await foreach prints each number as it is yielded without spaces.
🧠 Conceptual
intermediate
1:30remaining
Understanding IAsyncEnumerable behavior
Which statement about IAsyncEnumerable in C# is true?
AIAsyncEnumerable is a synchronous interface for streaming data.
BIAsyncEnumerable can only be used with synchronous foreach loops.
CIAsyncEnumerable requires all elements to be available before iteration starts.
DIAsyncEnumerable allows asynchronous iteration over a collection with await foreach.
Attempts:
2 left
💡 Hint
Think about how async streams help with data that arrives over time.
🔧 Debug
advanced
2: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);
        }
    }
}
ANo output, program hangs
BNullReferenceException
CInvalidOperationException
DCompilation error
Attempts:
2 left
💡 Hint
Look at where the exception is thrown inside the async stream.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines an async stream?
Select the code snippet that correctly defines an async stream method returning IAsyncEnumerable.
Aasync IAsyncEnumerable<int> StreamNumbers() { await Task.Delay(10); yield return 1; }
Basync Task<IAsyncEnumerable<int>> StreamNumbers() { yield return 1; }
CIAsyncEnumerable<int> StreamNumbers() { yield return 1; }
Dasync IAsyncEnumerable<int> StreamNumbers() { yield return 1; }
Attempts:
2 left
💡 Hint
An async stream method must be async and use yield return with await inside.
🚀 Application
expert
2: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++;
    }
}
A5
B3
C2
D4
Attempts:
2 left
💡 Hint
Count how many numbers from 0 to 4 are even.