Complete the code to declare an async stream method that returns integers.
public async [1]<int> GetNumbersAsync() { yield return 1; await Task.Delay(100); yield return 2; }
The method must return IAsyncEnumerable<int> to support async streams.
Complete the code to asynchronously iterate over the async stream.
await foreach (var number [1] GetNumbersAsync())
{
Console.WriteLine(number);
}The correct syntax for iterating over an async stream is await foreach (var item in collection).
Fix the error in the async stream method signature.
public [1] async IAsyncEnumerable<int> GenerateAsync() { yield return 1; await Task.Delay(100); yield return 2; }
The static modifier should come before async and the return type. The method must be declared as public static async IAsyncEnumerable<int> if static.
Fill both blanks to correctly yield values asynchronously with delay.
public async IAsyncEnumerable<int> CountAsync()
{
for (int i = 0; i [1] 3; i++)
{
await Task.Delay(100);
yield [2] i;
}
}The loop condition should be i < 3 and to yield values in async streams use yield return.
Fill all three blanks to filter and yield only even numbers asynchronously.
public async IAsyncEnumerable<int> EvenNumbersAsync(int[] numbers)
{
foreach (var num in numbers)
{
if (num [1] 2 [2] 0)
{
await Task.Delay(50);
yield [3] num;
}
}
}To check even numbers, use modulo operator % and compare with zero using ==. Yield values with yield return.