C# and .NET • Modern C# baseline
Asynchronous Programming with Task and async/await
Await I/O-bound operations without blocking a thread while the operation is incomplete.
Task represents an asynchronous operation and Task<T> represents an operation that produces a value. async/await makes continuation flow readable, but does not automatically make CPU work parallel. Await tasks instead of blocking with Result or Wait, which can deadlock or waste threads in application contexts.
Key points
- Use async Task for asynchronous methods and return the task to the caller.
- Use Task.Run for appropriate CPU-bound work, not as a wrapper around every I/O call.
Example
static async Task<string> LoadAsync(string path)
{
return await File.ReadAllTextAsync(path);
}
string text = await LoadAsync("notes.txt");
Console.WriteLine(text.Length);
What to expect
The file read does not synchronously block the calling thread while waiting for I/O.
Practice exercise
Propagate async through a call chain and identify where the top-level operation should observe failure.
Key idea: Asynchrony describes waiting; it is not synonymous with parallel execution.