C# await Keyword: Async Method Behavior
Understand how the C# await keyword suspends async methods, returns control to callers, and handles exceptions in async programming.

The C# await keyword is the core of asynchronous programming in C#. When you place await before a Task or Task<T> expression, the compiler rewrites the surrounding method into a state machine. The method returns an incomplete task to its caller at the first await that is not already complete, and the rest of the method runs as a continuation when the awaited operation finishes. This is not a blocking wait; it is a suspension that frees the calling thread to do other work.
How await Changes Method Execution
Consider a simple async method that reads a file:
public async Task<string> ReadFileAsync(string path)
{
string content = await File.ReadAllTextAsync(path);
return content;
}When the method reaches await File.ReadAllTextAsync(path), it checks whether the returned Task<string> is already complete. If the file read is still in progress, the method returns an incomplete Task<string> to its caller. The caller can continue executing until it awaits that returned task. Once the read completes, the continuation runs on the captured synchronization context (if one exists) or on a thread pool thread. The return value is then placed into the task returned by the method.
This suspension is the key difference from a synchronous call. A synchronous call blocks the current thread until the operation completes. await does not block; it yields control and lets the thread process other work. That is why await is safe to use on UI threads without freezing the interface.
The Relationship Between async and await
The await keyword is only valid inside a method marked with the async modifier. The async modifier signals the compiler to generate the state machine and allows the method to use await. An async method can return Task, Task<T>, ValueTask, ValueTask<T>, or, for async iterators, IAsyncEnumerable<T>. It can also return void, but async void is reserved for event handlers because exceptions from such methods cannot be caught by the caller.
public async Task<int> GetNumberAsync()
{
await Task.Delay(100);
return 42;
}The async keyword itself does not make the method run on a separate thread. It only enables await. The actual asynchronous behavior comes from the awaited operations. A method that contains no await will still run synchronously, but the compiler will warn about the missing await.
What Happens When You Await a Task
When you await a task, the compiler calls GetAwaiter() on that task to obtain an awaiter. The awaiter exposes an IsCompleted property, an OnCompleted method, and a GetResult method. If IsCompleted is true, the continuation is not scheduled; the method continues synchronously. If the task is incomplete, OnCompleted schedules the continuation. The continuation captures the current synchronization context unless the task was configured with ConfigureAwait(false).
await ReadFileAsync(path).ConfigureAwait(false);ConfigureAwait(false) tells the awaiter not to capture the synchronization context. This is often used in library code to avoid forcing continuations back to a UI thread or an ASP.NET request context. In a UI application, omitting ConfigureAwait(false) is usually correct because you need to update UI controls on the UI thread. In a console app or a background service, there is no synchronization context, so an incomplete awaited operation schedules its continuation on the thread pool whether or not you call ConfigureAwait(false).
Error Handling with await
An awaited task that faults throws an exception at the point of the await. This allows you to handle errors with the familiar try/catch pattern:
public async Task<string> FetchAsync(string url)
{
try
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
}
catch (HttpRequestException ex)
{
return $"Request failed: {ex.Message}";
}
}When a task faults, GetResult() rethrows the original exception. That exception is not wrapped in an AggregateException unless you explicitly block on the task with .Result or .Wait(). This is why await is preferred over blocking calls: it preserves the original exception type and stack trace. If you await a task that has been cancelled, a TaskCanceledException is thrown. You can catch it and handle cancellation separately.
Performance and Allocation Costs
The state machine generated by async/await has a cost. An async method that suspends can allocate a state machine box and a continuation delegate; the compiler tries to avoid some allocations when the awaited task is already complete. For hot paths that perform many small asynchronous operations, the overhead can matter. ValueTask and ValueTask<T> reduce allocations when the result is usually available synchronously.
public async ValueTask<int> GetValueAsync()
{
await Task.Yield();
return 1;
}ValueTask is useful for methods that often complete synchronously, such as reading from a cached value. But it has constraints: a ValueTask is designed to be awaited only once. If you need to pass it around or cache the result, call .AsTask() first.
Another performance concern is synchronization context capture. In ASP.NET Core, the default synchronization context is null, so ConfigureAwait(false) has no effect. In older ASP.NET or UI applications, capturing the context adds overhead and can cause deadlocks if you block on the task from a synchronous method. Avoid mixing blocking calls like .Wait() or .Result with await in the same code path.
Common Pitfalls and Misconceptions
One common mistake is assuming await makes the method run on a different thread. It does not. The method runs on the current thread until it hits an incomplete await. At that point the method returns, freeing the current thread to do other work, and the continuation runs on the thread pool or the captured context. If you need CPU-bound work to run on a background thread, use Task.Run explicitly.
Another pitfall is async void. Exceptions from an async void method are raised on the synchronization context and can crash the process. Use async Task for all methods except event handlers. If you must use async void, ensure you catch all exceptions inside the method.
Deadlocks occur when you block on an async method from a UI thread or a thread with a synchronization context. For example:
public void Button_Click()
{
var result = GetNumberAsync().Result; // Deadlock
}The .Result blocks the UI thread, but the continuation of GetNumberAsync needs the UI thread to complete. The method never finishes, so the UI thread stays blocked. The correct approach is to use await throughout the call chain. If you cannot make the entire chain async, use ConfigureAwait(false) inside the async method to avoid capturing the context, but that is a workaround rather than a solution.
Choosing When to Use await
Use await for I/O-bound operations such as file access, network calls, database queries, and HTTP requests. These operations spend most of their time waiting for external resources, and await lets the thread handle other work during that wait. For CPU-bound operations, await does not reduce the work or make it non-blocking by itself; the code still occupies a thread while it runs. If you need to offload that work to keep the caller responsive, use Task.Run and await the resulting task.
public async Task<int> ComputeAsync(int[] data)
{
return await Task.Run(() => HeavyComputation(data));
}Avoid using await in a tight loop that creates many small tasks, because the state machine overhead may exceed the benefit. Instead, consider batching or using Task.WhenAll to await multiple independent operations concurrently.
public async Task DownloadAllAsync(HttpClient client, IEnumerable<string> urls)
{
var tasks = urls.Select(url => client.GetStringAsync(url));
var results = await Task.WhenAll(tasks);
}Select creates the tasks immediately, and Task.WhenAll awaits all of them. This lets the HTTP requests overlap instead of waiting for each one to finish before starting the next. However, if any request fails, Task.WhenAll throws one of the exceptions after all the supplied tasks finish. Keep references to the individual tasks if you need to inspect partial failures.
The C# await keyword is a powerful tool, but it is not free. Understand the state machine, the synchronization context, and the allocation costs to write async code that is both correct and efficient. Prefer async/await over blocking calls, use ConfigureAwait(false) in library code, and reserve async void for event handlers. When you follow those rules, await makes asynchronous code readable and maintainable without sacrificing runtime behavior.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.