Asynchronous code keeps apps responsive while waiting for slow work (network, files, databases).
Mark a method async and return Task or Task<T>.
await waits for a task without blocking the thread.
Task.WhenAll() runs several tasks in parallel and waits for all.
💡 Key Takeaways: C# Async & Await
Mastering C# Async & Await helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
C# Async & Await Code Example
C#
static async Task<string> DownloadAsync(string name, int ms)
{
await Task.Delay(ms); // simulate network work
return $"{name} downloaded";
}
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = await Task.WhenAll(
DownloadAsync("report.pdf", 1000),
DownloadAsync("photo.jpg", 800),
DownloadAsync("data.csv", 600));
foreach (var r in results) Console.WriteLine(r);
Console.WriteLine($"Total ~{watch.ElapsedMilliseconds / 100 * 100} ms (in parallel)");
Output
report.pdf downloaded
photo.jpg downloaded
data.csv downloaded
Total ~1000 ms (in parallel)
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
C# Async & Await Practice Exercises
Check what you learned in “C# Async & Await”.
/ 2 solved
Fill in the blank
Mark the method as asynchronous.
static Task<string> DownloadAsync() { }
✓ Correct!Not quite. Try again.
Multiple choice
Which method runs several tasks and waits for all of them?