async / await Basics
In this page:
async / await Basics
async marks a method as asynchronous, allowing it to use await to pause execution at a slow operation without blocking the calling thread. Control returns to the caller while the awaited operation runs, then resumes the method when it completes. This keeps applications responsive during I/O-bound work like network or file access.
Note: An async method should return Task or Task<T> (or void only for event handlers), never block synchronously inside it.
Example: async / await Basics
using System;
using System.Threading.Tasks;
class Program
{
static async Task<int> ComputeAsync()
{
await Task.Delay(100);
return 42;
}
static async Task Main(string[] args)
{
Console.WriteLine("Starting...");
int result = await ComputeAsync();
Console.WriteLine($"Result: {result}");
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: