Challenge - 5 Problems
Task.WhenAny Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Task.WhenAny with different delays
What will be the output of this C# code snippet using
Task.WhenAny to await the first completed task?C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { var task1 = Task.Delay(300).ContinueWith(_ => "Task1"); var task2 = Task.Delay(100).ContinueWith(_ => "Task2"); var task3 = Task.Delay(200).ContinueWith(_ => "Task3"); var firstCompleted = await Task.WhenAny(task1, task2, task3); Console.WriteLine(await firstCompleted); } }
Attempts:
2 left
💡 Hint
Think about which task finishes first based on the delay times.
✗ Incorrect
Task2 has the shortest delay (100ms), so it completes first. Task.WhenAny returns the first completed task, so awaiting it yields "Task2".
🧠 Conceptual
intermediate1:30remaining
Understanding Task.WhenAny behavior
Which statement best describes what
Task.WhenAny does in C#?Attempts:
2 left
💡 Hint
Consider what happens if one task completes with an error before others finish.
✗ Incorrect
Task.WhenAny returns the first task that finishes, whether it completed successfully, faulted, or was canceled. It does not wait for all tasks or cancel others.🔧 Debug
advanced2:00remaining
Identify the runtime error with Task.WhenAny usage
What runtime error will this code produce when executed?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { Task<string> task1 = Task.FromResult("Hello"); Task<string> task2 = null; var first = await Task.WhenAny(task1, task2); Console.WriteLine(await first); } }
Attempts:
2 left
💡 Hint
Check what happens if one of the tasks passed to Task.WhenAny is null.
✗ Incorrect
Passing a null task to Task.WhenAny causes an ArgumentNullException at runtime because the method expects all tasks to be non-null.
❓ Predict Output
advanced2:00remaining
Output when first completed task faults
What will be the output of this code snippet?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { var task1 = Task.Run(() => throw new InvalidOperationException("Error in task1")); var task2 = Task.Delay(100).ContinueWith(_ => "Success"); var first = await Task.WhenAny(task1, task2); try { Console.WriteLine(await first); } catch (Exception ex) { Console.WriteLine(ex.GetType().Name); } } }
Attempts:
2 left
💡 Hint
Consider which task completes first and what happens when awaiting a faulted task.
✗ Incorrect
The task that throws an exception completes first. Awaiting it rethrows the exception, so the catch block prints the exception type name.
🚀 Application
expert3:00remaining
Using Task.WhenAny to implement a timeout
Which code snippet correctly uses
Task.WhenAny to implement a 500ms timeout for an asynchronous operation DoWorkAsync()? The code should print "Timeout" if the operation takes too long, or print the result if it completes in time.Attempts:
2 left
💡 Hint
Check which task finished first and print accordingly.
✗ Incorrect
Option A correctly compares the first completed task to the timeout task and prints "Timeout" if the timeout task finished first, otherwise prints the work result.