Member-only story

The Await vs ContinueWith in C# async programming

Understanding the Difference Between await and ContinueWith in C# Asynchronous Programming

Yohan Malshika
3 min readOct 10, 2024

In C#, asynchronous programming is used to improve the responsiveness of applications by allowing long-running operations to execute in the background while freeing up the UI thread for other tasks. Two important keywords used in asynchronous programming are await and ContinueWith. In this article, we will explore the differences between these two keywords and when to use them.

The await keyword

The await keyword is used to pause the execution of an asynchronous method until the awaited task completes. This allows the calling method to remain responsive while waiting for the task to finish. The await keyword can only be used in methods marked with the async modifier, and these methods return a Task or Task<TResult>.

Example:

public async Task<int> CalculateSumAsync(int a, int b)
{
int result = await Task.Run(() => Add(a, b));
return result;
}

In the example above, await is used to wait for the Task.Run() method to complete before returning the result of the addition operation.

The ContinueWith Keyword

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Yohan Malshika
Yohan Malshika

Written by Yohan Malshika

Software Engineer | .Net Developer | Technical Writer

Responses (3)

Write a response

Your implication is that there are behavioral differences between the two approaches, but that is not correct. The differences are purely syntactical. Anything you can express using async/await you can also express using ContinueWith, and vice versa.

Good Read :)
For C# newbies I would recommend sticking with await in most cases unless you 100%-ly know that you don't need to wait for the result and error handling is of no concern in your scenario. Better be save than spend hours troubleshooting, because your exception got lost in a ContinueWith().

Thanks for this breakdown. I find ContinueWith useful, however, it must be used with care with a detailed understanding of what runs when. The examples you provided are good contextual use cases.