C# Interview Question #61

What is asynchronous programming in C#?

Async/Await, Tasks & Multithreading Mid-Level Intermediate

Quick Interview Answer

Asynchronous programming allows a thread to avoid blocking while waiting for an operation such as database access, HTTP communication, file I/O, or another asynchronous resource.

Detailed Explanation

Asynchronous programming allows a thread to avoid blocking while waiting for an operation such as database access, HTTP communication, file I/O, or another asynchronous resource.

In C#, asynchronous code is primarily written with Task, Task<T>, async, and await. While an I/O operation is waiting, the thread can be returned to the runtime or server thread pool to perform other work.

This is especially important in ASP.NET Core because non-blocking I/O improves scalability. Asynchronous programming does not automatically mean that work runs on a separate thread; true asynchronous I/O can complete without dedicating a thread to the wait.

Code Example

public async Task<Product?> GetProductAsync(int id)
{
    return await _context.Products
        .FirstOrDefaultAsync(p => p.Id == id);
}