×

c# · level 3 · unit 6 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Start every task first, then await them all together.

Awaiting tasks one by one means each waits for the last: three 100 ms lookups take 300 ms. Run them , all in flight at the same time, and the waits overlap, so the whole job takes about 100 ms.

Start each task without await and keep the tasks. Then . It finishes when the last one does and hands back an array of results in the order you started them, never the order they finished. That keeps your output the same on every run.

using System;
using System.Linq;
using System.Threading.Tasks;

string[] towns = { "Leeds", "Bath", "York" };
Task<string>[] lookups = towns.Select(LookupAsync).ToArray();
string[] reports = await Task.WhenAll(lookups);
foreach (string report in reports)
{
    Console.WriteLine(report);
}

async Task<string> LookupAsync(string town)
{
    await Task.Delay(town.Length * 40);
    return $"{town}: {town.Length * 3} degrees";
}

towns.Select(LookupAsync) passes the method itself, the same as town => LookupAsync(town).