Async and Await in c#
Tags: csharp, async, await
Published: 2023-03-18 16:08:53

## Async and Await
In C#, the async and await keywords are used to create asynchronous code that can run in a non-blocking way. This allows our application to remain responsive and improves its performance.

**Why it is required?**
- We use async and await in C# when we need to perform potentially long-running operations that could block the calling thread, such as making web service calls, accessing databases, or reading and writing files.


Here's a simple example that demonstrates how to use async and await in C#:

```
using System;
using System.Net.Http;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main(string[] args)
    {
        // Create an instance of HttpClient
        HttpClient client = new HttpClient();

        // Call the GetAsync method asynchronously
        HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");

        // Read the response content as a string
        string responseContent = await response.Content.ReadAsStringAsync();

        // Display the response content in the console
        Console.WriteLine(responseContent);
    }
}

```

**Async Keyword**
- When a method is marked with the async keyword, it can be run asynchronously, which means it can be executed without blocking the calling thread.
- Instead, it can return control to the calling method immediately, while the asynchronous operation continues in the background.

**Await Keyword**
- The await keyword is used inside an async method to await the completion of an asynchronous operation.
- When the await keyword is encountered, the method is paused, and control is returned to the calling method until the asynchronous operation completes.

**Features**

- Non-blocking: async and await enable non-blocking execution of code, allowing other tasks to run while waiting for a potentially long-running operation to complete.
- Improved performance: By avoiding blocking threads, async and await can improve the performance of applications, especially in cases where there are many concurrent operations.
- Easy to use: async and await provide an easy-to-use syntax for writing asynchronous code that resembles synchronous code, making it easier to read and understand.
- Exception handling: async and await automatically handle exceptions thrown by asynchronous operations, making it easier to write robust code.
- Composition: async and await support composing asynchronous operations using tasks, making it easy to chain and combine asynchronous operations.
- Parallelism: async and await can be used with the Task Parallel Library (TPL) to enable parallel execution of asynchronous operations, which can further improve performance.

**When it is required?**
- We should use async and await when we need to improve the performance and scalability of our applications, especially in scenarios where there are many concurrent operations.
- They are particularly useful in applications that require a responsive user interface, as they allow us to perform long-running operations in the background without blocking the UI thread.