I need a clear Explanation About async and await in C# Can Anyone Elaborate deeply?
Loading
I need a clear Explanation About async and await in C# Can Anyone Elaborate deeply?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Aug 22, 2023, 1:48 AM EDT
In C#, the async and await keywords provide a convenient way to work with asynchronous code in a more readable and organized manner.
The async keyword is used to mark a method as asynchronous. An asynchronous method contains one or more await expressions and usually returns a Task or Task object. This indicates that the method can execute asynchronously and won't block the calling thread while it's waiting for certain operations to complete.
The await keyword is used within an asynchronous method to pause the execution of that method until the awaited task is complete. The task being awaited must be an instance of a type that represents an asynchronous operation, such as Task, Task, or other awaitable types.
Here's an example:
public async Task MainAsync() { Console.WriteLine("Starting MainAsync"); await OperationAsync(); Console.WriteLine("MainAsync Completed"); } public async Task OperationAsync() { Console.WriteLine("OperationAsync Started"); // Simulate a delay await Task.Delay(TimeSpan.FromSeconds(2)); Console.WriteLine("OperationAsync Completed"); } public static void Main() { var task = MainAsync(); task.Wait(); // Waiting for MainAsync to complete }In this example MainAsync & OperationAsync is marked as asynchronous with the async keyword. OperationAsync method includes a simulated delay using Task.Delay. Within MainAsync, the await OperationAsync() call will pause the execution of MainAsync until OperationAsync completes.
The async and await are not meant to make your code run faster; they're designed to make your application more responsive by better utilizing resources and allowing concurrent execution of tasks that might otherwise block the main thread.
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 24, 2023, 12:32 AM EDT
asyncandawait:async: Theasynckeyword is used to mark a method as asynchronous. This indicates that the method contains asynchronous operations and will likely use theawaitkeyword to manage them.await: Theawaitkeyword is used within anasyncmethod to pause the method's execution and asynchronously wait for the completion of an asynchronous operation. It allows the method to yield control back to the calling thread while the awaited operation is performed in the background.How
asyncandawaitWork Together: When you mark a method with theasynckeyword, you can useawaitto indicate points where you want to pause the method's execution and await the completion of asynchronous tasks. While awaiting a task, the calling thread is free to perform other tasks, making efficient use of system resources.Benefits:
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 22, 2023, 2:41 AM EDT
https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/
https://dotnettutorials.net/lesson/async-and-await-operator-in-csharp/
Vishal YelveRank #178 · 13.6K reputationPosted Aug 22, 2023, 3:32 AM EDT
Hi Hari,
do refer below links
https://www.bytehide.com/blog/async-await-csharp
https://dotnettutorials.net/lesson/async-and-await-operator-in-csharp/
Kudvenket Video - https://www.youtube.com/watch?v=C5VhaxQWcpE
Shivprasad koirala Sir /Questpond video - https://www.youtube.com/watch?v=V2sMXJnDEjM&pp=ygUVYXN5bmMgYW5kIGF3YWl0IGluIGMj
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 23, 2023, 9:18 AM EDT
The async keyword is used to for asynchronous. An asynchronous method has more await expressions
The await keyword is used within an asynchronous method to pause the execution of that method until the awaited task is complete.