In C#, what does the async and await keyword pair actually do, and why is awaiting an I/O operation different from just blocking the thread?
technical-conceptual · Intern level · software-engineering
What the interviewer is really asking
Probes whether the candidate understands async/await as compiler-generated continuations that free the thread during I/O, rather than believing await spins up a new thread or runs code in parallel.
What to say
- Explain the mechanism: the compiler rewrites an async method into a state machine, and at each await it signs up the rest of the method as a continuation and returns control to the caller instead of running line by line to the end.
- Make the thread point concrete: while an awaited I/O operation (a database call, an HTTP request) is in flight, no thread sits and waits; the thread is returned to the pool to do other work, and the method resumes on a continuation when the operation completes.
- Contrast with blocking: a synchronous blocking call holds the thread until it finishes, so under load you exhaust threads, while async lets one thread serve many in-flight I/O operations, which is why it scales better.
What to avoid
- Don't say async runs your code on a new thread or in parallel; for I/O it's about freeing the current thread, and no extra thread is doing the waiting.
- Don't claim await makes code faster; a single operation takes the same wall-clock time, the benefit is throughput and responsiveness because threads aren't blocked.
- Don't describe await as 'pausing the whole program'; only that method is suspended, and the caller keeps running until it needs the result.
Example answers
Strong: async and await let the compiler turn the method into a state machine. When I await an I/O call, the method sets up the rest of itself as a continuation and hands the thread back, so the thread isn't stuck waiting on the network or disk. When the operation finishes, the method picks up where it left off. Blocking would keep that thread parked the whole time, so under load you'd run out of threads, whereas async keeps them free to handle other work.
Weak: async and await run your method on a background thread so it doesn't freeze the program. The await keyword waits for that thread to finish in parallel, which is why async code is faster than normal code.