Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Task.WhenAll for parallel execution in C Sharp (C#) - Interactive Code Practice

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

Complete the code to run two tasks in parallel and wait for both to finish.

C Sharp (C#)
var task1 = Task.Run(() => Console.WriteLine("Task 1"));
var task2 = Task.Run(() => Console.WriteLine("Task 2"));
await [1](task1, task2);
Drag options to blanks, or click blank then click option'
ATask.WhenAll
BTask.WaitAll
CTask.RunAll
DTask.WaitAny
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which is synchronous and blocks the thread.
Using Task.WaitAny which waits for only one task to complete.
2fill in blank
medium

Complete the code to start three tasks and wait for all to complete using Task.WhenAll.

C Sharp (C#)
var tasks = new List<Task> {
    Task.Run(() => DoWork(1)),
    Task.Run(() => DoWork(2)),
    Task.Run(() => DoWork(3))
};
await [1](tasks);
Drag options to blanks, or click blank then click option'
ATask.WaitAll
BTask.WhenAll
CTask.RunAll
DTask.WaitAny
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the list to Task.WaitAll which is synchronous.
Using Task.WaitAny which waits for only one task.
3fill in blank
hard

Fix the error in the code to correctly wait for multiple tasks to complete asynchronously.

C Sharp (C#)
var taskA = Task.Run(() => Compute(10));
var taskB = Task.Run(() => Compute(20));
Task.WhenAll(taskA, taskB).Wait(); // Fix this line
Console.WriteLine("All tasks done");
Drag options to blanks, or click blank then click option'
Aawait Task.WhenAny(taskA, taskB);
BTask.WaitAll(taskA, taskB);
CTask.WhenAny(taskA, taskB);
Dawait Task.WhenAll(taskA, taskB);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread.
Using Task.WhenAny which waits for only one task.
4fill in blank
hard

Fill both blanks to create a dictionary with task results after all tasks complete.

C Sharp (C#)
var tasks = new List<Task<int>> {
    Task.Run(() => 1),
    Task.Run(() => 2),
    Task.Run(() => 3)
};
var results = await [1](tasks);
var dict = results.[2]((result, index) => new { index, result }).ToDictionary(x => x.index, x => x.result);
Drag options to blanks, or click blank then click option'
ATask.WhenAll
BSelect
CSelectIndexed
DSelectMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using SelectMany which flattens collections incorrectly.
Using a non-existent method SelectIndexed.
5fill in blank
hard

Fill in the blanks to run tasks in parallel, filter results greater than 5, and create a list of those results.

C Sharp (C#)
var tasks = new List<Task<int>> {
    Task.Run(() => 3),
    Task.Run(() => 7),
    Task.Run(() => 10)
};
var results = await [1](tasks);
var filtered = results.Where(x => x [2] 5).ToList();
Console.WriteLine(string.Join(", ", filtered));
Drag options to blanks, or click blank then click option'
ATask.WhenAll
B>
C<
DTask.WaitAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread instead of awaiting.
Using '<' which filters results less than 5.

Practice

(1/5)
1.

What does Task.WhenAll do in C#?

easy
A. Runs multiple tasks at the same time and waits for all to finish
B. Runs tasks one after another in sequence
C. Cancels all running tasks immediately
D. Runs only the first task and ignores others

Solution

  1. Step 1: Understand Task.WhenAll purpose

    Task.WhenAll is designed to run multiple tasks in parallel and wait until all complete.
  2. Step 2: Compare options with behavior

    Only Runs multiple tasks at the same time and waits for all to finish correctly describes running tasks simultaneously and waiting for all to finish.
  3. Final Answer:

    Runs multiple tasks at the same time and waits for all to finish -> Option A
  4. Quick Check:

    Task.WhenAll = parallel run and wait [OK]
Hint: Think: all tasks run together and wait for all done [OK]
Common Mistakes:
  • Thinking tasks run one by one
  • Believing it cancels tasks
  • Assuming it runs only one task
2.

Which of the following is the correct syntax to wait for multiple tasks task1 and task2 to complete using Task.WhenAll?

?
easy
A. await Task.WhenAll{task1, task2};
B. await Task.WhenAll(task1, task2);
C. await Task.WhenAll[task1, task2];
D. await Task.WhenAll();

Solution

  1. Step 1: Recall Task.WhenAll syntax

    Task.WhenAll takes tasks as parameters inside parentheses and returns a Task to await.
  2. Step 2: Check each option's syntax

    Only await Task.WhenAll(task1, task2); uses parentheses correctly and awaits the combined task.
  3. Final Answer:

    await Task.WhenAll(task1, task2); -> Option B
  4. Quick Check:

    Correct syntax uses parentheses () [OK]
Hint: Use parentheses () to pass tasks to WhenAll [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Using curly braces {} incorrectly
  • Using angle brackets <> wrongly
3.

What will be the output of this C# code?

using System;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        Task<int> t1 = Task.FromResult(5);
        Task<int> t2 = Task.FromResult(10);
        int[] results = await Task.WhenAll(t1, t2);
        Console.WriteLine(string.Join(",", results));
    }
}
medium
A. Compilation error
B. 10,5
C. 5,10
D. Task[]

Solution

  1. Step 1: Understand Task.FromResult and Task.WhenAll

    Task.FromResult creates completed tasks with values 5 and 10. Task.WhenAll waits and returns results as an int array.
  2. Step 2: Check output of Console.WriteLine

    Results array contains [5, 10], so joining with comma prints "5,10".
  3. Final Answer:

    5,10 -> Option C
  4. Quick Check:

    Task.WhenAll returns array of results [OK]
Hint: Task.WhenAll returns array of results in order [OK]
Common Mistakes:
  • Assuming results are reversed
  • Expecting a string instead of array
  • Thinking code causes error
4.

Identify the error in this code snippet using Task.WhenAll:

async Task RunTasks() {
    var t1 = Task.Delay(1000);
    var t2 = Task.Delay(2000);
    Task.WhenAll(t1, t2);
    Console.WriteLine("Done");
}
medium
A. Missing await before Task.WhenAll, so tasks run but not waited
B. Task.Delay cannot be used with Task.WhenAll
C. Console.WriteLine should be inside Task.WhenAll
D. Task.WhenAll requires a list, not separate tasks

Solution

  1. Step 1: Check usage of Task.WhenAll

    Task.WhenAll returns a Task that must be awaited to wait for all tasks to finish.
  2. Step 2: Identify missing await

    Code calls Task.WhenAll but does not await it, so "Done" prints immediately before delays finish.
  3. Final Answer:

    Missing await before Task.WhenAll, so tasks run but not waited -> Option A
  4. Quick Check:

    Always await Task.WhenAll to wait [OK]
Hint: Always await Task.WhenAll to wait for tasks [OK]
Common Mistakes:
  • Not awaiting Task.WhenAll
  • Thinking Task.Delay can't be used
  • Misplacing Console.WriteLine
5.

You want to run three tasks that return strings: Task<string> t1, t2, and t3. You want to get all results as a list of strings after all finish. Which code correctly does this?

hard
A. var list = await Task.WhenAll(new List { t1, t2, t3 });
B. var list = await Task.WhenAll(t1, t2, t3).ToList();
C. var list = Task.WhenAll(t1, t2, t3).Result.ToList();
D. var results = await Task.WhenAll(t1, t2, t3); var list = results.ToList();

Solution

  1. Step 1: Understand Task.WhenAll return type

    Task.WhenAll returns a string array when awaited on Task<string> tasks.
  2. Step 2: Convert array to list correctly

    var results = await Task.WhenAll(t1, t2, t3); var list = results.ToList(); awaits Task.WhenAll, then converts the array to a list using ToList(), which is correct.
  3. Final Answer:

    var results = await Task.WhenAll(t1, t2, t3); var list = results.ToList(); -> Option D
  4. Quick Check:

    Await Task.WhenAll then convert array to list [OK]
Hint: Await Task.WhenAll first, then convert results to list [OK]
Common Mistakes:
  • Trying to call ToList() before awaiting
  • Using .Result causing blocking
  • Passing list directly without awaiting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard