Your Ultimate async / await Tutorial in C#


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

There are some libraries in .Net Framework 4.5, that has a built-in support for asynchronous programming using asyncawait keywords.

These libraries, such as the HttpClient, StreamWriter, StreamReader and many more, provide you async methods that you can use in your program directly with await.

They have been added with the async/await capabilities because these provide functionalities that require long-running operations such as reading large file or download huge resources.

So the asyncawait will significantly improve the performance of your application while not blocking the UI or the execution.

I/O Bound async / await

Let's take an example for using the HttpClient's GetStringAsync method, that will hit a provided URL and return the html response content from it and display its length on Console screen:

Click run to see the console output
·using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace AsyncApp
class Program
private const string URL = "";
static void Main(string[] args)
var someTask = DoSomethingAsync();
someTask.Wait(); //this is a blocking call, use it only on Main method
public static void DoSynchronousWork()
// You can do whatever work is needed here
Console.WriteLine("1. Doing some work synchronously");
static async Task DoSomethingAsync() //A Task return type will eventually yield a void
Console.WriteLine("2. Async task has started...");
await GetStringAsync(); // we are awaiting the Async Method GetStringAsync
static async Task GetStringAsync()
using (var httpClient = new HttpClient())
Console.WriteLine("3. Awaiting the result of GetStringAsync of Http Client...");
string result = await httpClient.GetStringAsync(URL); //execution pauses here while awaiting GetStringAsync to complete
//From this line and below, the execution will resume once the above awaitable is done
//using await keyword, it will do the magic of unwrapping the Task<string> into string (result variable)
Console.WriteLine("4. The awaited task has completed. Let's get the content length...");
Console.WriteLine($"5. The length of http Get for {URL}");
Console.WriteLine($"6. {result.Length} character");
static void DoSynchronousWorkAfterAwait()
//This is the work we can do while waiting for the awaited Async Task to complete
Console.WriteLine("7. While waiting for the async task to finish, we can do some unrelated work...");
for (var i = 0; i <= 5; i++)
for (var j = i; j <= 5; j++)
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content