100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C# Async/Await Cheat Sheet

C# Async/Await Cheat Sheet

Explains async/await in C#, Task combinators like WhenAll and WhenAny, cancellation tokens, exception handling, and common concurrency pitfalls to avoid.

2 PagesIntermediateApr 5, 2026

Basic Async Method

Declaring and awaiting an async method.

csharp
public async Task<string> GetDataAsync(){    using var client = new HttpClient();    string result = await client.GetStringAsync("https://api.example.com/data");    return result;   // Task<string> is returned to the caller automatically}// Calling itstring data = await GetDataAsync();

Task Combinators

Running and coordinating multiple async operations.

csharp
Task<int> t1 = ComputeAsync(1);Task<int> t2 = ComputeAsync(2);int[] results = await Task.WhenAll(t1, t2);   // Run concurrently, wait for bothTask firstDone = await Task.WhenAny(t1, t2);  // Wait for whichever finishes firstawait Task.Delay(1000);                       // Non-blocking async waitvar cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));await LongRunningAsync(cts.Token);             // Pass token for cooperative cancellation

Exception Handling & ConfigureAwait

Catching async exceptions and library-code best practices.

csharp
try{    await DoWorkAsync();}catch (HttpRequestException ex){    Console.WriteLine($"Request failed: {ex.Message}");}// In library code, avoid capturing the calling contextpublic async Task<int> LibraryMethodAsync(){    var result = await SomeIoAsync().ConfigureAwait(false);    return result;}

Rules & Gotchas

Patterns to follow and traps to avoid.

  • async void- Only for event handlers; exceptions can't be awaited/caught by the caller — avoid it elsewhere
  • Task vs Task<T>- Task represents a void async operation; Task<T> represents one that returns a value
  • Deadlocks- Calling .Result or .Wait() on an async method from a UI/ASP.NET context can deadlock
  • ConfigureAwait(false)- Skips resuming on the original synchronization context; commonly used in library code
  • Async all the way- Avoid mixing blocking calls with async code in the same call chain
  • CancellationToken- Should be threaded through every async method that can run for a while
  • Task.Run- Offloads CPU-bound work to the thread pool; don't use it for I/O-bound async calls
Pro Tip

Never call .Result or .Wait() on a Task in code that has a synchronization context (like ASP.NET or WPF) — it can deadlock because the awaited continuation is stuck waiting for the same thread.

Was this cheat sheet helpful?

Explore Topics

#CAsyncAwait#CAsyncAwaitCheatSheet#Programming#Intermediate#BasicAsyncMethod#TaskCombinators#ExceptionHandlingConfigureAwait#RulesGotchas#Concurrency#ErrorHandling#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet