Beginner’s Guide to Async and Await in C# (With Code Examples)

Claudio Bernasconi
Claudio Bernasconi
hero image

Ever noticed how some apps stay smooth and responsive, even when fetching data or handling complex tasks? You’re clicking, scrolling, and things just work.

The magic behind this experience often comes down to asynchronous programming, and the good news is, C# has two fantastic tools for this: async and await.

If you want to build a C# app that’s super fast, with no delays or freezing screens, then learning how to use async/await is essential. In this guide, I’ll walk you through how it works, why it matters, and how it can transform your app’s user experience.

Let’s dive in!

Sidenote: If you want to learn more about C#, (as well asynchronous programming like we’re covered in this article), then check out my C#/.NET Bootcamp course.

learn c sharp

This is the only course you need to learn C# programming and master the .NET platform.

No previous coding experience required - you’ll learn C# programming from scratch, including powerful skills like data structures, object-oriented programming (OOP), and testing. All while building your own projects, so you can get hired as a C#/.NET Developer in 2025!

Check it out above, or watch the first few videos here for free.

With that out of the way, let’s get into this 5-minute tutorial…

What is asynchronous programming, and why does it matter?

Imagine you’re cooking a meal. You’ve got a pot of water on to boil, but instead of standing there staring at it, you’re chopping veggies, marinating the meat, and getting everything prepped. Makes sense, right? You’re not just waiting around; you’re multitasking.

Asynchronous programming works the same way in code. Sometimes, a task—like fetching data from a server or writing to a file—can take time. If you handle it synchronously, meaning the program has to finish one task before moving to the next, it’s like standing over that pot, waiting for it to boil. Everything else stops until it’s done.

But with asynchronous programming, or “async” for short, you’re free to handle other tasks while waiting for that data or file to load. So, instead of freezing up, your app stays active and responsive, keeping things moving for the user. This approach is crucial whenever you’re dealing with tasks that can take an unpredictable amount of time.

This effect is apparent when working with user interfaces. You want to execute long-running tasks in the background to free up the UI thread. The UI thread should never be blocked because you want the app to remain responsive to button clicks and other user interactions.

TL;DR

Async programming is a key part of creating software that feels smooth and responsive. It’s especially important for apps where user experience matters, like web apps, mobile apps, or anything that relies on real-time data updates.

And as I mentioned above - the good news is that in C#, you’ve got async and await to make this easy to set up.

Getting to know `async` and `await`

In C#, async and await are like partners in crime. Together, they make async programming possible, and they change how your code flows, especially when you’re dealing with tasks that take time. Think of things like fetching data from a server or reading a large file.

Here’s how it works.

What does `async` do?

When you mark a method with async, you’re telling C# that this method might take a while to finish. Rather than holding up your entire application, async lets this method do its work in the background, keeping everything else responsive.

But there’s a catch—async can’t do all the heavy lifting on its own. That’s where await steps in.

What does `await` do?

Imagine await as a traffic light in your code. When your program hits await, it pauses right at that spot and signals, "Wait here while I handle this task in the background, but keep the other traffic (other tasks) moving."

So while that specific task is paused, the rest of your app keeps running. Once the task finishes, your code picks right up where it left off, just like when the light turns green.

For example

Imagine you’re writing code to fetch data from an API. Here’s a synchronous version, meaning the code runs line by line, waiting for each step to complete before moving on:

public string GetData()
{
	// This blocks the app until the data is fetched
	return FetchDataFromServer();

}

In this setup, the app stops everything until it gets the data back. But with async and await, you can let this happen in the background:

public async Task<string> GetDataAsync()
{
	// This happens in the background, keeping your app responsive
	return await FetchDataFromServerAsync();

}

Now, the method returns a Task<string> instead of just a string. That’s because async methods typically return a Task (or Task<T> if there’s a specific return type), which represents the ongoing work.

Adding async marks this method as asynchronous, and using await with FetchDataFromServerAsync() tells it to wait for the task to complete—but without stopping the rest of your app.

Cool right?

It’s important that the method we want to await is correctly implemented. In the examples shown above, the FetchDataFromServer method will return a string, while the FetchDataFromServerAsync method will return a Task<string>. Otherwise, we cannot use the await keyword when calling the method.

Now that you understand the basics let’s explore where async and await really shine in real-world applications.

Putting `async` and `await` to work with real-life scenarios

Now that you know how async and await work together to make async programming possible, let’s look at how you’d use them in real applications - such as calling an external API, reading or writing to a file, or connecting to a database.

This is where they really shine.

Scenario #1. Fetching data from an API

Imagine you’re building a weather app that fetches the latest weather data from a remote server. A synchronous call here would freeze your app until the server responds, which could take a few seconds (or longer if the network’s slow).

But with async and await, you can handle this task in the background, keeping your app responsive and the user engaged.

Here’s how you might set it up:

public async Task<string> GetWeatherDataAsync()
{
	using (HttpClient client = new HttpClient())
	{
    	// This call runs in the background, keeping the app free to handle other tasks
    	return await client.GetStringAsync("https://api.weather.com/data");

	}
}

With await client.GetStringAsync(...), you’re telling C# to fetch the data but to let your app continue doing other things while it waits for the response. Once the data is ready, this method picks up right where it left off and returns the result.

This way, your app stays responsive even if the data takes a moment to arrive, giving your user a smooth experience.

Scenario #2. Loading files in the background

Loading files, especially large ones, can also benefit from async programming. Imagine you’re building a document viewer that needs to load a large text file. Without async, your app would pause until the file finishes loading. But by running it asynchronously, you can keep the app responsive.

Here’s an example:

public async Task<string> ReadFileAsync(string filePath)
{
	using (StreamReader reader = new StreamReader(filePath))
	{
    	return await reader.ReadToEndAsync();

	}
}

In this code, await reader.ReadToEndAsync() reads the file’s content in the background. If the file’s large, this can take a second, but because it’s running asynchronously, your app stays responsive.

This gives you a seamless experience—the user can still interact with the app while the file loads.

Scenario #3. Async data processing

Async isn’t only useful for external resources. Imagine processing a large list of data that could slow down your app if handled on the main thread. Running it asynchronously lets you perform heavy processing without holding up the rest of the app.

public async Task<List<int>> ProcessDataAsync(List<int> data)
{
	// Simulate data processing in the background
	await Task.Delay(2000); // Mimics a delay for processing
	return data.Select(x => x * 2).ToList(); // Example transformation
}

With async, you can handle data-heavy operations without slowing down the app, ensuring a smooth user experience.

This type of programming gives the computer the opportunity to place the background task on one of the available cores while the rest of the application runs on its primary core. This enables a more efficient usage of the available system resources.

Common mistakes and best practices

Async programming is powerful, but it has its quirks that can trip you up, especially if you’re just starting out.

Here, I’ll walk you through some common mistakes to avoid and best practices that can make your async code more reliable and easier to manage.

Mistake #1. Blocking async code with .Result or .Wait()

A frequent pitfall is using .Result or .Wait() to retrieve the result of an async method. Doing this turns async code back into synchronous code, blocking the calling thread until the async task finishes.

This approach not only undermines the whole point of async programming but can also lead to deadlocks — especially in UI apps.

Avoid this:

// This blocks the calling thread until the async task completes
var result = GetDataAsync().Result;

Instead, whenever possible, use await to keep your async code truly asynchronous:

// This maintains async flow and avoids blocking
var result = await GetDataAsync();

If you find you need to call async methods in synchronous code, consider restructuring your code to make it fully async-friendly.

Mistake #2. Using async void outside of Event Handlers

It’s tempting to use async void for methods, but unlike async Task methods, async void doesn’t allow you to wait for the task to complete or to handle exceptions. This can lead to unpredictable behavior and hard-to-diagnose bugs if errors slip by unnoticed.

The only place where async void makes sense is in event handlers, like button clicks in UI code, where there’s no need for a return type.

Stick with async Task for most async methods:

// Instead of async void
public async Task GetDataAsync()
{
	// your async code here
}

To keep your code predictable and manageable, reserve async void solely for event handlers:

// Async void in an event handler is acceptable
private async void Button_Click(object sender, EventArgs e)
{
	await GetDataAsync();
}

Mistake #3. Skipping ConfigureAwait(false) in library code

When writing libraries or backend code, it’s easy to overlook ConfigureAwait(false), but not using it can lead to unnecessary context switching, which slows things down.

Why?

Well, by default, async methods try to resume in the same context they started in, which isn’t usually necessary for library code. However, in UI applications, you’d want to avoid ConfigureAwait(false) since UI updates need to run on the main thread.

In general, use ConfigureAwait(false) in non-UI libraries or backend code to improve performance:

public async Task FetchDataAsync()
{
	var data = await GetDataFromDatabaseAsync().ConfigureAwait(false);
	// Code after this will continue without switching contexts unnecessarily
}

Mistake #4. Not handling exceptions properly

Exceptions in async code don’t behave exactly like they do in synchronous code. This means that without proper handling, async errors can pass by unnoticed, leading to silent failures or unexpected crashes down the line.

It’s a good habit to wrap async calls in try-catch blocks to catch and handle exceptions gracefully:

public async Task FetchDataAsync()
{
	try
	{
    	var data = await FetchDataFromServerAsync();
	}
	catch (HttpRequestException ex)
	{
    	Console.WriteLine($"Error fetching data: {ex.Message}");
	}
}

Mistake #5. Writing async code sequentially

Async code flows differently from synchronous code, and trying to write it in a purely sequential style can make it harder to read and debug - mainly because async tasks don’t always complete in order. This means that structuring your code to make the async flow clear is incredibly important.

So always design your async code with readability in mind:

  • Use meaningful method names (like GetDataAsync)
  • Break down long methods, and
  • Avoid deeply nesting async calls

For example

Here you can see that instead of deeply nested async methods, I've separated the steps into smaller async methods:

public async Task ProcessDataAsync()
{
	var data = await FetchDataAsync();
	var processedData = await ProcessDataAsync(data);
	await SaveDataAsync(processedData);
}

Not only will this run smoother, but keeping your async code clean and organized makes it easier to follow and work with at a glance.

Mistake #6. Inconsistent method naming

When naming async methods in C#, it’s common practice to add the suffix “Async” (like GetDataAsync) to any method that returns a Task<T>. This helps indicate the method is asynchronous and can be awaited.

However, if you prefer to omit the “Async” suffix, consistency is key. Choose one approach and apply it across your entire codebase. Switching between “Async” and non-suffixed names makes it harder to tell whether a method is synchronous or asynchronous without checking its return type.

Clear, consistent naming not only makes your code easier to read but also prevents confusion when calling methods.

Now it's your turn! Try out `async` and `await` in your own projects today

Async programming is all about creating smoother, faster experiences—keeping your app responsive while handling time-consuming tasks in the background.

By using async and await, you eliminate the frustration of frozen screens and lag, making your application feel fluid and enjoyable to use.

Now it’s time to see the impact for yourself. Try creating a small app or refactoring an existing one with async methods, and watch how it transforms the experience. Async programming isn’t just a technical choice; it’s a way to build apps that users love. So, give it a try—jump in, experiment, and see your app come to life in a whole new way!

P.S.

Don’t forget - if you want to learn more about C# and the .NET platform, (as well asynchronous programming like we’ve covered in this article) then check out my complete course:

learn c sharp

No previous coding experience required - you’ll learn C# programming from scratch, including powerful skills like data structures, object-oriented programming (OOP), and testing. All while building your own projects, so you can get hired as a C#/.NET Developer in 2025!

It’s the only course you need to learn C# programming and master the .NET platform. You’ll learn everything from scratch and put your skills to the test with exercises, quizzes, and projects!

Plus, once you join, you'll have the opportunity to ask questions in our private Discord community from me, other students, and working developers.


There's always someone online 24/7 happy to help. It's by far the thing that my students always tell me is the best part of their experience. Hope you decide to take my course and if you do, make sure to come say hi on Discord!

Check it out above, or watch the first few videos here for free.

More from Zero To Mastery

5 Reasons Why You Should Learn C# preview
5 Reasons Why You Should Learn C#

It's been 23 years since C# went live, but it's still growing in popularity. Find out why (+ why you should learn this language ASAP to advance your career).

What Is C# Used For? Pretty Much Everything! preview
What Is C# Used For? Pretty Much Everything!

C# can build anything from Desktop + Web Apps to Cloud Integrations, Games, Automations + more. In this guide, I walk through why it works so well in each area.

Beginner's Guide to C# String Interpolation (With Code Examples) preview
Beginner's Guide to C# String Interpolation (With Code Examples)

Unlock the power of C# string interpolation to write cleaner, more efficient code. Learn how to embed variables and format strings effortlessly!