C# Tutorial: Async/Await and Concurrency
async/await, Task, Task<T>, parallel execution, cancellation, and thread safety in C#.
The async/await Pattern
Mark a method `async` to use `await` inside it. Awaiting a `Task` releases the thread until the work completes:
```csharp
using System.Net.Http;
static async Task<string> FetchPageAsync(string url) {
using var client = new HttpClient();
return await client.GetStringAsync(url);
}
// Call with await at the top level
string html = await FetchPageAsync("https://example.com");
Console.WriteLine(html[..100]);
```
---
Task and Task\<T\>
```csharp
// Fire-and-forget
Task.Run(() => Console.WriteLine("Background work"));
// With return value
Task<int> task = Task.Run(() => 42);
int result = await task;
// Delay
await Task.Delay(1000); // non-blocking sleep
```
---
Running Tasks in Parallel
```csharp
var t1 = FetchPageAsync("https://example.com");
var t2 = FetchPageAsync("https://microsoft.com");
// Wait for both concurrently
var results = await Task.WhenAll(t1, t2);
Console.WriteLine($"Got {results.Length} pages");
```
---
CancellationToken
Allow long-running operations to be cancelled:
```csharp
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
try {
await DoWorkAsync(cts.Token);
} catch (OperationCanceledException) {
Console.WriteLine("Timed out");
}
static async Task DoWorkAsync(CancellationToken ct) {
for (int i = 0; i < 100; i++) {
ct.ThrowIfCancellationRequested();
await Task.Delay(100, ct);
}
}
```
---
lock — Thread Safety
```csharp
class Counter {
private int _count;
private readonly object _lock = new();
public void Increment() {
lock (_lock) {
_count++;
}
}
public int Value => _count;
}
```
---
Parallel.ForEach
CPU-bound parallel work:
```csharp
var items = Enumerable.Range(1, 1000).ToList();
Parallel.ForEach(items, item => {
// heavy computation per item
});
```
---
What's Next?
You can write non-blocking, high-performance code. Next: **testing** — unit tests with xUnit, assertions, and test-driven development.
What you'll learn in this C# async/await and concurrency tutorial
This interactive C# tutorial has 5 hands-on exercises. Estimated time: 15 minutes.
- async / await Basics — Write an `async Task<string>` method `GetMessageAsync()` that awaits `Task.Delay(0)` and returns `"Hello async!"`. Await…
- Task.Run — Use `Task.Run` to compute `6 * 7` on a background thread, await the result, and print it.
- Task.WhenAll — Create two tasks that each return a string (`"A"` and `"B"`). Use `Task.WhenAll` to run them in parallel and print the c…
- Task.Delay (Non-blocking Sleep) — Print `"Start"`, await `Task.Delay(0)` (instant in tests), then print `"End"`. Both lines should appear.
- Returning Multiple Tasks — Create a method `async Task<int> SquareAsync(int n)` that awaits `Task.Delay(0)` and returns `n * n`. Await calls for `3…
C# Async/Await and Concurrency concepts covered
- The async/await Pattern
- Task and Task\<T\>
- Running Tasks in Parallel
- CancellationToken
- lock — Thread Safety
- Parallel.ForEach
- What's Next?