wasAsked.

async/await Explanation: Can you explain what async/await is in .NET?

Dodge Construction Network · Senior Developer · technical

Published

Asked in 2026 · .NET, C#

Community answers

Posted by wasAsked editorial collection

An async method runs synchronously until it reaches an incomplete await. It then returns a Task and schedules the remainder as a continuation. Await does not automatically create a thread; genuine asynchronous I/O can wait without occupying an application thread. Example: public async Task<Order?> FindAsync(Guid id, CancellationToken ct) { return await db.Orders.AsNoTracking() .SingleOrDefaultAsync(x => x.Id == id, ct); } Use async all the way, pass cancellation, and avoid .Result or .Wait(), which block threads and can cause deadlocks in context-bound applications or thread-pool starvation in servers. Return Task rather than async void except for event handlers.

Backbencher Developer · · 0 likes

async and await are C# keywords that make asynchronous code look like normal sequential code. They let a program start a long-running I/O operation, free the current thread while waiting, and continue when the result is ready. Basic example: public async Task<string> GetWeatherAsync(string city, CancellationToken ct) { using var response = await _http.GetAsync($"/weather/{city}", ct); // thread is released here response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(ct); } How it works: - An async method returns Task, Task<T> or ValueTask<T>. - At await, if the operation has not completed, the method's remaining code is registered as a continuation and control returns to the caller. No thread sits waiting. - When the I/O completes, the continuation runs (in ASP.NET Core, on a thread-pool thread). - The compiler turns the method into a state machine to make this possible. Why it matters: - Web servers: while one request waits for the database, its thread can serve other requests, so the server handles far more concurrent requests with the same resources. - Desktop and mobile apps: the UI thread stays responsive. Important distinctions: - Asynchronous is not the same as parallel. await waits for one operation without blocking; Task.WhenAll runs independent operations concurrently: var (user, orders) = (GetUserAsync(id), GetOrdersAsync(id)); await Task.WhenAll(user, orders); - Async helps I/O-bound work. CPU-bound work still needs a thread; use Task.Run only when you deliberately offload it (not in ASP.NET Core actions). Best practices: - Go async all the way; never block with .Result or .Wait() (it can cause deadlocks or thread-pool starvation). - Avoid async void except for event handlers. - Pass CancellationToken through the call chain. - Use ConfigureAwait(false) in general-purpose libraries. - Name async methods with the Async suffix.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics