The Await vs ContinueWith in C# async programming

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

Yohan Malshika

--

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

--

--