CSharp · Console
using System;
using System.Diagnostics;
using System.Threading.Tasks;
public class Program
{
static async Task<int> GetAsync(int x)
{
await Task.Delay(1000);
return x;
}
public static async Task Main()
{
// A: sequential - each call starts only after the previous one finishes
var sw = Stopwatch.StartNew();
var a = await GetAsync(1);
var b = await GetAsync(2);
Console.WriteLine($"Α: {sw.ElapsedMilliseconds}");
// B: concurrent - both calls start first, then we await them
sw.Restart();
var t1 = GetAsync(1);
var t2 = GetAsync(2);
var c = await t1;
var d = await t2;
Console.WriteLine($"Β: {sw.ElapsedMilliseconds}");
}
}