Async and await keywords of C# were introduced in C# 5.0. They were designed to make it easier to write asynchronous code, which can run in the background while other code is executing.
The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. When you mark a method as async, you can use the "await" keyword to indicate that the method should wait for the result of an asynchronous operation before continuing.
Use of 'async' and 'await' in C#
Asynchronous programming is a programming technique that allows code to be executed concurrently without blocking the execution of the calling thread. In other words, asynchronous code can run in the background while other code is executing. In synchronous programming, each line of code is executed sequentially, and the program waits for each operation to complete before moving on to the next one. This can lead to performance problems, particularly in programs that need to perform long-running operations like I/O or network requests.
Asynchronous programming allows programs to perform these operations without blocking the calling thread. When an asynchronous operation is started, the program continues to execute other code while it waits for the operation to complete. The program is notified when the operation is complete and can continue with the following line of code.
Asynchronous programming can be implemented using various techniques, such as callbacks, events, and promises. In C#, the "async" and "await" keywords provide a convenient way to write asynchronous code that looks similar to synchronous code, making it easier to read and maintain.
We will get all the benefits of traditional Asynchronous programming with much less effort with the help of async and await keywords.
Suppose we are using two methods as, Method1 and Method2, respectively, and both methods are not dependent on each other, and Method1 takes a long time to complete its task. In Synchronous programming, it will execute the first Method1, wait for the completion of this method, and then execute Method2. Thus, it will be a time-intensive process even though both methods are not depending on each other.
We can run all the methods parallelly using simple thread programming, but it will block UI and wait to complete all the tasks. To come out of this problem, we have to write too many codes in traditional programming, but if we use the async and await keywords, we will get the solutions in much less code.
Also, we will see more examples, and if any third Method, as Method3 has a dependency on method1, it will wait for the completion of Method1 with the help of await keyword.
Async and await in C# are the code markers that mark code positions from where the control should resume after completing a task.
Let's start with practical examples for understanding the programming concept.
Code examples of C# async await
We are going to take a console application for our demonstration.
Example 1
In this example, we will take two methods that are not dependent on each other.
Code sample
class Program
{
static void Main(string[] args)
{
Method1();
Method2();
Console.ReadKey();
}
public static async Task Method1()
{
await Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(" Method 1");
// Do something
Task.Delay(100).Wait();
}
});
}
public static void Method2()
{
for (int i = 0; i < 25; i++)
{
Console.WriteLine(" Method 2");
// Do something
Task.Delay(100).Wait();
}
}
}
In the code above, Method1 and Method2 are not dependent on each other, and we call from the Main method.
Here, we can see Method1 and Method2 are not waiting for each other.
Output
Regarding the second example, suppose we have Method3, which depends on Method1.
Example 2
In this example, Method1 returns the total length as an integer value, and we pass a parameter as a length in Method3, which comes from Method1.
Here, we have to use await keyword before passing a parameter in Method3, and for it, we have to use the async keyword from the calling method.
If we use C# 7 or less, we cannot use the async keyword in the Main method for the console Application because it will give the error below.

Jacek MarchelPosted Mar 24, 2023, 4:50 PM
I'm confused by the example. Exactly the same output would be produced by code that is not using async await mechanism. The example does not explain anything.