Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to…

Follow publication

Member-only story

Understanding async and await in C#

Yohan Malshika
Stackademic
Published in
3 min readNov 6, 2024

--

Understanding async and await in C#

Asynchronous programming is essential in C#. It helps your app run faster and more smoothly. The async and await keywords make writing asynchronous code easier and more readable.

In this article, we’ll discuss why async and await are valuable, how they work, and how to use them effectively in your code.

Why Use async and await?

When your code waits for something to finish like reading data from a file, waiting for a network response, or querying a database. It can get stuck. During this “waiting time,” other parts of your program can still do useful work. This is where asynchronous programming comes in.

async and await let you handle these "waiting tasks" without blocking the entire program. They let other code continue to run until the waiting part finishes.

Basics of async and await

  • async keyword: This tells C# that a method is asynchronous. It goes before the method return type. For example:
public async Task DoSomethingAsync() { }
  • await keyword: This keyword pauses the method until the awaited task is done. When the task finishes, the method resumes where it left off. You…

--

--

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Yohan Malshika

Software Engineer | .Net Developer | Technical Writer

No responses yet

Write a response