What is the async and await pattern in C#? When should you use it?
Ask by ICSM Computer
Updated 12 Jun 2025
The
asyncandawaitpattern in C# is a language feature used to write asynchronous, non-blocking code in a clean, readable way.Instead of using callbacks or threads manually, you can write asynchronous code as if it were synchronous, while still freeing up threads to do other work (like serving more web requests).
Basic Concept
asynckeyword: Marks a method as asynchronous.awaitkeyword: Tells the compiler to pause execution until the awaited task completes.Behind the scenes, the method gets "split" into parts that run before and after the
await. The thread is returned to the pool while waiting.Components of the Pattern
asyncTask,Task<T>, orValueTask<T>awaitTask/Task<T>Rules to Remember
awaitinside a method marked withasync.asyncmethod must return:Task(if it returns nothing),Task<T>(if it returns a value),void(only for event handlers).awaitonly works withTask,Task<T>,ValueTask, orcustom awaitables.Example: Asynchronous Delay
This method does not block the thread during the delay.
When Should You Use
async/await?Use it when:
Don’t use it when:
Task.Run()without a good reasonawaitinside — this will show a compiler warningBad Example
Better:
Rule of Thumb