C# Async/Await: What Is It for and Why Does It Make Things Easier?

C# Async/Await: What Is It for and Why Does It Make Things Easier?

Asynchronous programming might sound complicated, but it’s quite simple: it’s a way to run long tasks without “freezing” the rest of the program. These are often called long-running operations — like accessing a database, loading large files, or making an API call. So, how does this help?

Think of it like cooking a big meal: while something is baking in the oven, you can chop vegetables or start another recipe. In code, this would be like running other parts of your program while a task is “baking” in the background.

In C#, async/await makes this process much easier. It lets your program keep doing other things while waiting for the task to finish, and when the result is ready, it picks up right where it left off, without extra hassle.


Before and After async/await'

Before: Running a long operation without freezing the program required complex techniques. Here’s an example:

public void ProcessData()
{
    Task.Run(() =>
    {
        var data = FetchLongData();
        UpdateScreen(data);
    });
}        

After: With async/await, the same code is much cleaner and easier to understand:

public async Task ProcessDataAsync()
{
    var data = await FetchLongDataAsync();
    UpdateScreen(data);
}        

What’s Happening with await?

In the second example, await lets the program “wait” for FetchLongDataAsync() without actually stopping everything else. The code keeps going, with the long task “running” in parallel. When the response arrives, it picks back up and updates the screen. This keeps the program more agile and responsive, especially if multiple things are happening at once.


Another Example: Parallel Execution

Beyond handling a long task while the rest of the program continues, async/await also allows tasks to run in parallel! For example, if your program needs to do two internet searches and then combine the results, you can do that in parallel:

public async Task ProcessImagesAndReportAsync()
{
    var imageTask = DownloadImagesAsync();  // Starts downloading images
    var reportTask = ProcessReportAsync(); // Starts the report at the same time

    await Task.WhenAll(imageTask, reportTask); // Waits for both to finish
    Console.WriteLine("Images and report are ready!");
}        

Here, await Task.WhenAll() waits for both tasks to complete before moving on. This makes the code run much faster than doing each task one at a time!


Benefits of Using async/await

With async/await, you get:

  • Faster applications: The program doesn’t get “stuck” on a single long-running task;
  • Cleaner, straightforward code: async/await makes your code easier to read and maintain, replacing more complex techniques used to run tasks without stopping the program;
  • Parallel execution: You can run multiple operations simultaneously, making better use of time and resources.

Getting Started

To use async/await in your C# code:

  1. Add the async keyword at the beginning of the method (async Task);
  2. Use await in front of the long-running task, like await FetchLongDataAsync();.


Have you used async/await in your work? How has it helped make your code more efficient?

Mohsin Khan

Lead Developer @ LiSEC | 8k+ LinkedIn | Software Architect | .NET Content Creator

6mo

If you're aiming to develop scalable applications in .NET, async/await is the way to go! Thanks for sharing these insights, Cássio 👏.

Rafael Andrade

Senior Data Engineer | Azure | AWS | Databricks | Snowflake | Apache Spark | Apache Kafka | Airflow | dbt | Python | PySpark | Certified

6mo

Appreciate this great content, Cássio Huggentobler de Costa.

Vanessa Cardoso da Rocha

Frontend Developer | Web Accessibility | Wordpress and Shopify

6mo

Your posts about C# and its application in day-to-day use are very helpful. Congratulations! 🚀

Like
Reply
André Ramos

Senior Software Engineer | Java | Spring Boot | Angular | Micro Services | AWS | Fullstack Software Developer | TechLead

6mo

Excellent explanation and explanation of the theme! Congratulations!

Like
Reply

To view or add a comment, sign in

More articles by Cássio Huggentobler de Costa

Insights from the community

Others also viewed

Explore topics