C# Async Await Interview Questions
Last updated 2026-11-19
Async and await look easy to use and are hard to explain. Interviewers ask what the compiler does, why async void is dangerous, how a deadlock happens when you call .Result, and what order lines print in when a method hits its first await.
The answers use .NET 8 and C# 12, including newer APIs such as Parallel.ForEachAsync and Task.WaitAsync. The server-side relevance is discussed on the .NET Core page. For plain threads, compare with Java multithreading.
| Core types | Task, Task<T>, ValueTask, CancellationToken |
|---|---|
| Language version | C# 12 on .NET 8 |
| Key rule | Async all the way, never block on a Task |
The model in four sentences
An async method runs synchronously until its first await on an incomplete task. At that point the compiler-generated state machine saves its locals and returns a Task to the caller. When the awaited operation completes, the continuation resumes, on the captured context if there is one or on a thread-pool thread otherwise. No thread is blocked while waiting for I/O.
C# Async Await Interview Questions: questions with worked answers
1 What do async and await actually do? Does await create a thread?
easyasync tells the compiler to turn the method into a state machine and allows await inside it. await checks whether the awaited task is complete; if so it continues synchronously, otherwise it registers the rest of the method as a continuation and returns control to the caller. It does not create a thread. For I/O, completion comes from the operating system via I/O completion ports, so no thread waits at all. Task.Run is what moves CPU work onto the thread pool.
2 Predict the output order.
mediumThe call to Foo runs synchronously up to its first await, so C prints before the caller continues. After B, awaiting the task lets the delay finish and D prints last. The main idea is that calling an async method is not the same as awaiting it.
csharpConsole.WriteLine("A"); Task t = Foo(); Console.WriteLine("B"); await t; static async Task Foo() { Console.WriteLine("C"); await Task.Delay(100); Console.WriteLine("D"); } // A, C, B, D3 Why is async void bad, and when is it acceptable?
mediumAn async void method cannot be awaited, so the caller cannot know when it finishes, and an exception thrown in it is raised on the synchronisation context or crashes the process instead of being captured in a Task. It also complicates testing. The only accepted use is event handlers, which must return void. Everywhere else return Task.
csharp// BAD: caller cannot catch the exception static async void Fire() { await Task.Delay(1); throw new Exception("lost"); } // GOOD static async Task RunAsync() { await Task.Delay(1); throw new Exception("caught by awaiter"); } try { await RunAsync(); } catch (Exception e) { Console.WriteLine(e.Message); }4 How does the classic .Result deadlock happen, and does it occur in ASP.NET Core?
hardIn a UI app or classic ASP.NET, the code after await resumes on a single captured context. If the calling code blocks that context with .Result or .Wait(), the continuation cannot run, and the blocked caller waits for it forever: a deadlock. ASP.NET Core has no synchronisation context, so this exact deadlock does not occur, but blocking still wastes pool threads and can cause thread-pool starvation. The rule remains: async all the way up.
csharp// WinForms or old ASP.NET: deadlock string Load() => LoadAsync().Result; async Task<string> LoadAsync() { await Task.Delay(10); return "ok"; }5 What does ConfigureAwait(false) do, and when should you use it?
mediumIt tells await not to resume on the captured context, so the continuation can run on any pool thread. Library code should use it to avoid forcing the caller context and to prevent deadlocks when consumers block. Application code, such as UI event handlers that touch controls, needs the context and should not. In ASP.NET Core there is no context to capture, so it has little effect there.
csharpvar data = await http.GetStringAsync(url).ConfigureAwait(false); // continues on a thread-pool thread, so do not touch UI controls here6 Task.WhenAll versus Task.WhenAny, and how are exceptions reported?
mediumWhenAll returns a task that completes when all inputs have, producing an array of results, and when awaited it throws the first exception only, although all exceptions are available on the task's Exception property (an AggregateException). WhenAny completes when the first input completes and returns that task, which is useful for timeouts and racing. Starting tasks first and awaiting them together runs them concurrently.
csharpvar t1 = Task.Delay(300).ContinueWith(_ => 1); var t2 = Task.Delay(100).ContinueWith(_ => 2); var winner = await Task.WhenAny(t1, t2); Console.WriteLine(await winner); // 2 int[] all = await Task.WhenAll(t1, t2); Console.WriteLine(string.Join(",", all)); // 1,27 Sequential versus concurrent awaits: what is the timing difference?
mediumAwaiting each call in turn runs them one after the other, so two 1-second operations take about 2 seconds. Starting both before awaiting, or using WhenAll, lets them overlap, so they take about 1 second. This is one of the most common async performance bugs.
csharp// about 2 seconds await Task.Delay(1000); await Task.Delay(1000); // about 1 second var a = Task.Delay(1000); var b = Task.Delay(1000); await Task.WhenAll(a, b);8 When do you use Task.Run, and why not in ASP.NET Core handlers?
mediumTask.Run queues CPU-bound work to the thread pool so a UI thread stays responsive. In a web request handler it only moves work to another pool thread, adding scheduling overhead without increasing capacity, since the same pool serves requests. Do not wrap I/O in Task.Run; use the real async API instead.
9 What is ValueTask and when should you use it?
mediumValueTask<T> is a struct that avoids a Task allocation when an operation often completes synchronously, such as a cache hit. Its restrictions: await it only once, never await it concurrently, and do not use .Result before completion. Use it on hot paths measured to allocate; otherwise return Task<T>, which is safer.
csharpprivate readonly Dictionary<int,string> _cache = new(); public ValueTask<string> GetAsync(int id) => _cache.TryGetValue(id, out var v) ? new ValueTask<string>(v) : new ValueTask<string>(LoadAsync(id)); async Task<string> LoadAsync(int id) { await Task.Delay(10); return _cache[id] = "v" + id; }10 How does cancellation work with CancellationToken?
mediumCancellation is cooperative. A CancellationTokenSource creates a token that you pass down to async calls; when you call Cancel, operations that observe the token throw OperationCanceledException. Check token.ThrowIfCancellationRequested() in loops and pass the token to HttpClient, EF Core and Task.Delay. ASP.NET Core supplies HttpContext.RequestAborted when the client disconnects.
csharpusing var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); try { await Task.Delay(5000, cts.Token); } catch (OperationCanceledException) { Console.WriteLine("cancelled"); }11 What is IAsyncEnumerable and await foreach?
mediumIAsyncEnumerable<T> models a sequence whose items arrive asynchronously, such as pages from an API or rows streamed from a database. An async iterator uses async and yield return; the consumer uses await foreach, and can add a CancellationToken with WithCancellation or the [EnumeratorCancellation] attribute.
csharpstatic async IAsyncEnumerable<int> Numbers() { for (int i = 1; i <= 3; i++) { await Task.Delay(10); yield return i; } } await foreach (var n in Numbers()) Console.Write(n); // 12312 Task.Delay versus Thread.Sleep.
easyThread.Sleep blocks the current thread for the duration, wasting it. Task.Delay returns a task that completes after the delay without blocking any thread, so you await it in async code. Use Thread.Sleep only in dedicated synchronous worker threads or tests.
13 Why can you not await inside a lock statement, and what replaces it?
mediumA lock is tied to the thread that took it, but an await may resume on a different thread, and the compiler therefore forbids await inside lock. For async mutual exclusion use SemaphoreSlim with WaitAsync and Release in a finally block, or restructure so that the critical section is synchronous.
csharpprivate readonly SemaphoreSlim _gate = new(1, 1); async Task UpdateAsync() { await _gate.WaitAsync(); try { await SaveAsync(); } finally { _gate.Release(); } }14 How do you run many async operations in parallel with a limit?
mediumParallel.ForEachAsync (.NET 6) takes a MaxDegreeOfParallelism option and a CancellationToken. Older code uses a SemaphoreSlim gate around each task. Unbounded Task.WhenAll over thousands of items can overload the remote service or exhaust sockets.
csharpawait Parallel.ForEachAsync(urls, new ParallelOptions { MaxDegreeOfParallelism = 4 }, async (url, ct) => await http.GetStringAsync(url, ct));15 What happens to exceptions in an unawaited task?
mediumThe exception is stored in the Task and is never observed if nobody awaits it, so the failure is silently lost; since .NET 4.5 an unobserved task exception no longer crashes the process by default. Always await tasks, or attach error handling, and use a hosted service with try and catch for background loops.
16 What does TaskCompletionSource do?
hardIt creates a Task that you complete manually with SetResult, SetException or SetCanceled, which lets you wrap callback or event-based APIs in await-friendly form. Create it with TaskCreationOptions.RunContinuationsAsynchronously to prevent continuations running inline inside SetResult.
csharpstatic Task<string> FromCallback(Action<Action<string>> register) { var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); register(result => tcs.SetResult(result)); return tcs.Task; }
Frequently asked questions
Is async the same as multithreading?
No. Async is about not blocking while waiting; the work may involve zero threads for I/O. Multithreading runs code concurrently on several threads.
What should an async method return?
Task or Task<T>, ValueTask in measured hot paths, and void only for event handlers.
Does async make code faster?
It improves scalability by freeing threads during I/O, not the speed of a single operation.
Where do I learn threads in the Java world for comparison?
Related guides
More in Programming Language Interview Questions by Language
- C# Coding Interview Questions
- C# Collections Interview Questions
- C# Delegates and Events Interview Questions
- C# Interview Questions and Answers
- C# LINQ Interview Questions
- C# OOPs Interview Questions
- C++ Coding Interview Questions
- C++ Interview Questions and Answers
- C++ Memory Management Interview Questions
- C++ OOPS Interview Questions
- C++ Pointers and References Interview Questions
- C++ Smart Pointers Interview Questions