How to use async and await in .Net Core

Follow these best practices when using asynchronous programming in .Net Core to build more performant and scalable applications

Asynchronous programming allows you to write programs that don’t block on each statement or instruction, meaning the computer can move on to other tasks before waiting for previous tasks to finish. As a result, asynchronous programming enables you to build applications that are more scalable and responsive.

The async and await keywords allow us to write asynchronous code. These have been optimized in .Net Core for ease of use and performance. This article discusses a few points that you should be aware of when working with asynchronous programming in .Net Core applications.

The async and await keywords

An asynchronous method is one that is marked with the async keyword in the method signature. It can contain one or more await statements. It should be noted that await is a unary operator — the operand to await is the name of the method that needs to be awaited. The point at which the await keyword is encountered is known as the suspension point. The following code snippet illustrates how the async and await keywords are used.

public async Task GetDataAsync()
{
  using (SqlConnection connection = new
  SqlConnection(connectionString)
  {
     await connection.OpenAsync();
     //Other code   
  }
}

To continue reading this article register now