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

Task.WhenAny for first completion in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a task that returns 5.

C Sharp (C#)
var task = Task.Run(() => [1]);
Drag options to blanks, or click blank then click option'
AConsole.WriteLine(5)
B"5"
CTask.Delay(1000)
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using Console.WriteLine inside the lambda instead of returning a value.
Returning a string instead of an integer.
2fill in blank
medium

Complete the code to await the first task to complete among two tasks.

C Sharp (C#)
var firstFinished = await Task.WhenAny([1], Task.Delay(1000));
Drag options to blanks, or click blank then click option'
ATask.Run(() => 10)
BTask.Delay(2000)
CTask.CompletedTask
DTask.Run(() => Console.WriteLine("Hi"))
Attempts:
3 left
💡 Hint
Common Mistakes
Using a delay longer than the other task, so it never finishes first.
Using a task that does not complete or returns void.
3fill in blank
hard

Fix the error in the code to get the result of the first completed task.

C Sharp (C#)
var firstFinished = await Task.WhenAny(task1, task2);
var result = firstFinished.[1];
Drag options to blanks, or click blank then click option'
AResult
BGetResult()
CResultAsync
DGetAwaiter()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method that does not exist.
Using GetAwaiter() without awaiting.
4fill in blank
hard

Fill both blanks to create two tasks and await the first to complete.

C Sharp (C#)
var task1 = Task.Run(() => [1]);
var task2 = Task.Run(() => [2]);
var first = await Task.WhenAny(task1, task2);
Drag options to blanks, or click blank then click option'
A100
BTask.Delay(500).Wait(); return 200;
CTask.Delay(1000).Wait(); return 300;
DConsole.WriteLine("Done")
Attempts:
3 left
💡 Hint
Common Mistakes
Using Console.WriteLine which returns void.
Making both tasks delay the same time.
5fill in blank
hard

Fill all three blanks to create tasks and get the result of the first completed task.

C Sharp (C#)
var task1 = Task.Run(() => [1]);
var task2 = Task.Run(() => [2]);
var first = await Task.WhenAny(task1, task2);
var result = first.[3];
Drag options to blanks, or click blank then click option'
ATask.Delay(1000).Wait(); return 42;
B500
CResult
DTask.Delay(200).Wait(); return 24;
Attempts:
3 left
💡 Hint
Common Mistakes
Accessing the result before awaiting the task.
Using wrong property or method to get the result.