C# Practice Exercises

Concurrent Tasks with WhenAll

Run independent asynchronous operations concurrently.

Introduction

Task.WhenAll completes when every supplied task has finished.

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]);
    }
}
Result of running the code
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.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges