Skip to content

async and await

Context: The async and await keywords are the core of asynchronous programming in C#. They allow you to write non‑blocking code that looks like synchronous code.

  • async marks a method as asynchronous, allowing the use of await.
  • await suspends the method until the awaited task completes, without blocking the thread.
public async Task ProcessDataAsync()
{
Console.WriteLine("Start");
await Task.Delay(1000); // non‑blocking wait
Console.WriteLine("After delay");
}

Web API controllers: Mark actions as async and await database calls, HTTP calls, or file I/O.

Example: ASP.NET Core async actions improve scalability.