C# Asynchronous programming using async-await

In synchronous programming code is executed sequentially from top to bottom. Thus,execution of the next statement is blocked until the current code execution is complete which could be a blocking call like I/O operation.

To overcome this bottleneck, asynchronous programming plays an important role by keeping the application responsive and executing the long running tasks in background.

Async-await keywords are introduced in C#5 and are supported by .Net Framework 4.5 or higher.

Sample code to demonstrate the usage of async-await to keep the application responsive during the long running task:

If you look at the compiled code using the Reflector, you can see State Machine in the generated code and thus keeping it simple for the developer.

To support the Aysnc programming, .Net provides certain APIs which  can utilized by the developers for long running tasks. For example HttpClient, StreamReader, StreamWriter, etc.

One important thing to note, using async without await will cause the method to work as synchronous method.