C# Interview Question #150

What are common async/await mistakes in C#?

Advanced Async, Parallelism & Threading Senior Advanced

Quick Interview Answer

Common mistakes include blocking on tasks with .Result or .Wait(), using async void for methods other than event handlers, forgetting to await tasks, starting excessive concurrent operations, and ignoring cancellation.

Detailed Explanation

Common mistakes include blocking on tasks with .Result or .Wait(), using async void for methods other than event handlers, forgetting to await tasks, starting excessive concurrent operations, and ignoring cancellation.

Another mistake is using Task.Run to wrap naturally asynchronous I/O. If a database or HTTP API already provides an asynchronous method, it should normally be awaited directly.

Developers should also avoid running concurrent EF Core operations on the same DbContext instance because DbContext is not thread-safe.

The general production guideline is async all the way for I/O-bound operations, return Task rather than async void, propagate CancellationToken where useful, and control concurrency deliberately.

Code Example

// Preferred
public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}

// Avoid:
// _context.SaveChangesAsync().Wait();
// var result = SomeAsync().Result;