Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Async Method: How async and await Work

Learn how async and await work in C#: return types, exception handling, cancellation, performance tradeoffs, and common pitfalls.

~4 min readC#
#async/await#Task#asynchronous programming#C# concurrency
Illustration of a C# async method using await to offload work to a background task while the caller continues.

When you write a C# async method, you are not creating a new thread. The async keyword marks a method so that it can use await to yield control while an operation completes. This article explains what async and await actually do, how to choose return types, handle exceptions, support cancellation, and avoid common mistakes.

What async and await Actually Do

The async keyword does not change the method's execution thread. It only enables the use of await inside the method. When the runtime encounters an await expression, it checks whether the awaited operation has already completed. If it has, execution continues synchronously. If not, the method returns an incomplete task to the caller, and the continuation is scheduled when the operation finishes.

csharp
public async Task<string> FetchDataAsync(HttpClient client, string url) { string content = await client.GetStringAsync(url); return content; }

In this example, GetStringAsync returns a Task<string>. If that task has not already completed, the await expression suspends FetchDataAsync, and the method returns an incomplete Task<string> to the caller immediately. If the task is already complete, execution continues synchronously. Either way, the caller can continue doing other work or await the returned task.

Choosing the Right Return Type

A C# async method can return Task, Task<T>, ValueTask<T>, or void. The void return type is reserved for event handlers because exceptions from a void async method cannot be awaited and will be posted to the synchronization context or thread pool.

Return typeWhen to use
TaskAsync operation with no result
Task<T>Async operation that returns a value
ValueTask<T>Hot-path methods where the result is often available synchronously
voidEvent handlers only

For most library code, use Task or Task<T>. ValueTask<T> can reduce allocations when the method frequently completes synchronously, but it is more restrictive: consume an instance only once and avoid blocking on it with .Result or .AsTask().Wait().

Handling Exceptions in Async Methods

Exceptions thrown inside an async method are captured and stored on the returned task. They are not thrown directly to the caller unless the caller awaits the task. This means you must handle exceptions after an await, not around the method call itself.

csharp
public async Task<string> ReadFileAsync(string path) { using var reader = new StreamReader(path); return await reader.ReadToEndAsync(); } // Caller try { string text = await ReadFileAsync("config.json"); } catch (FileNotFoundException ex) { Console.WriteLine($"Missing file: {ex.FileName}"); }

If you call an async method without awaiting it, exceptions are stored in the returned task and can raise the TaskScheduler.UnobservedTaskException event if the task is garbage collected without being observed. Always await async methods or explicitly handle their tasks.

Supporting Cancellation

Long-running async methods should accept a CancellationToken to allow cooperative cancellation. The token is passed to underlying async APIs, which may throw OperationCanceledException when cancellation is requested.

csharp
public async Task DownloadAsync(HttpClient client, string url, CancellationToken token) { byte[] data = await client.GetByteArrayAsync(url, token); await File.WriteAllBytesAsync("download.bin", data, token); }

When a cancellation token is triggered, the async method should stop its work and let the OperationCanceledException propagate. Do not swallow this exception unless you have a specific reason. If you need to perform cleanup after cancellation, use a finally block.

Performance and Threading Implications

An await does not block the calling thread. It returns control to the caller, allowing the thread to process other work. This is especially important for UI applications and high-concurrency servers. However, every await introduces state machine overhead: the compiler generates a struct that stores local variables and continuation logic. In most applications this overhead is negligible, but in extremely hot loops you may want to avoid unnecessary await calls.

Another common misconception is that async methods always run on a background thread. They do not. The method runs synchronously until the first incomplete await. If you need to offload CPU-bound work, use Task.Run explicitly, but be aware that it adds thread pool scheduling overhead.

Common Pitfalls and How to Avoid Them

One frequent mistake is blocking on an async method using .Result or .Wait(). This can cause deadlocks in environments with a synchronization context, such as UI applications or ASP.NET Classic. Instead, use await all the way up the call stack.

Another issue is using async void outside event handlers. A void async method cannot be awaited, making error handling difficult. Return Task or Task<T> from normal methods; use ValueTask<T> only when its allocation benefits and single-consumption restrictions are appropriate.

Finally, be careful with async methods that do not contain an await. The compiler will warn about this, and the method body runs synchronously and returns an already completed task. If you intended to return a completed task, use Task.CompletedTask or Task.FromResult<T> instead of async without await.

csharp
public Task<int> GetValueAsync() { return Task.FromResult(42); // No async needed }

Understanding these behaviors helps you write C# async methods that are reliable, responsive, and maintainable in production code.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.