Async and Await In C#

Mohamed Hashish
2 min readJul 16, 2021

--

With asynchronous programming, you can divide your logic into awaitable tasks, where you can perform some long running operations without having to block the execution of your application.

.Net framework gives us simple and easy-to-use keywords which are the async and await modifiers to transform your code from synchronous to asynchronous.

So you can call an async method separately by obtaining a task object to it,
then do some unrelated work, and after that await that task, either it has
already finished which will result in returning the value from the task, or if it has not finished yet the program will execute normally without blocking.

Understanding Async and Await In C# by an example :

Suppose and we want to prepare a breakfast and we have three-component (coffee, toast, eggs)
Suppose that:
-The time spent to prepare eggs is 6 second
-The time spent to prepare toast is 3 second

Now we have two ways to make the breakfast (Synchronous and Asynchronous )

First: synchronous
1- We will pour coffee
2- We will start preparing eggs which will take 6 second
3- Then start preparing toast which will take 3 second
Total time will be about 9 seconds

Second: asynchronous
1-We will pour coffee
2-Start preparing eggs and toast at the same time
Total will be about 6 seconds

The code :

The Code link on GitHub

https://github.com/MohamedHashish42/AsyncAndAwaitInCSharp

For more detail

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

--

--