C# Practice Exercises

Concurrent Tasks with WhenAll

Task.WhenAll completes when every supplied task has finished.

How it works

Run independent asynchronous operations concurrently.

Starting both tasks before awaiting them allows their delays to overlap.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Threading.Tasks;
class Program
{
    static async Task<int> Fetch(int value) { await Task.Delay(20); return value; }
    static async Task Main()
    {
        int[] results = await Task.WhenAll(Fetch(20), Fetch(22));
        Console.WriteLine(results[0] + results[1]);
    }
}
Expected output
42

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.