C#
Asynchronous Programming
6 question(s)
What do async and await do?
Beginner
await asynchronously waits for a Task, releasing the thread until it completes, then resumes the method; async marks a method that uses await and returns a Task.
public async Task<string> LoadAsync(HttpClient h) => (await h.GetStringAsync("/x")).Trim();
Real-world example
A web server frees the request thread during a DB call so it can serve other requests.
What is the difference between Task and Task<T>?
Beginner
Task represents async work with no result; Task<T> completes with a value of type T that you get by awaiting it.
Task done = Task.Delay(100);
Task<int> n = Task.FromResult(42);
int v = await n;
Real-world example
Returning Task<Order> from a repository so callers can await the fetched order.
How do you run multiple async operations concurrently?
Intermediate
Start the tasks without awaiting each immediately, then await Task.WhenAll to run them in parallel and wait for all.
var t1 = h.GetStringAsync(a);
var t2 = h.GetStringAsync(b);
var r = await Task.WhenAll(t1, t2);
Real-world example
Fetching price, stock and reviews at once to cut page latency.
What does ConfigureAwait(false) do?
Intermediate
It resumes the continuation without capturing the synchronization context, avoiding a needless hop and reducing deadlocks. Use it in library code that doesn't touch UI/HttpContext.
var body = await h.GetStringAsync(url).ConfigureAwait(false);
Real-world example
A reusable NuGet library uses it so apps that block on it don't deadlock.
Why can .Result or .Wait() deadlock, and what's the fix?
Advanced
In a single-threaded context the blocked thread waits for a continuation that needs that same thread — neither proceeds. Fix by awaiting all the way down.
// risky: var d = LoadAsync().Result;
var d = await LoadAsync();
Real-world example
A legacy MVC action blocking on an async call hangs under load until made async end-to-end.
How do you design for scalability and avoid thread-pool starvation?
Advanced
Use async I/O so request threads return to the pool while waiting; never block on async (sync-over-async) as it holds threads and starves the pool under load.
public async Task<IActionResult> Get() => Ok(await _repo.GetAsync());
Real-world example
Removing blocking .Result calls let a checkout API handle far more concurrent users.