Back
Close

Your Ultimate async / await Tutorial in C#

AramT
175.5K views

Let's take a look at this example of a CPU-bound asynchronous method.

Click run to see the console output
·using System;
using System.Threading.Tasks;
namespace AsyncApp
{
class Program
{
static void Main(string[] args)
{
var totalAfterTax = CalculateTotalAfterTaxAsync(70);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In the above program, we have added an async method, CalculateTotalAfterTaxAsync, that takes an argument of float which represents the value to calculate upon.

Inside the method, if you notice, we are awaiting a task created on a background thread. As mentioned previously, we use this methodology, CPU Bound asynchronous method, when we need to do some offline heavy calculation that doesn't rely on specific I/O operation.

Once the task is completed. it will resume the CalculateTotalAfterTaxAsync with the statements after the await line. So it will displaying the Total after calculating the tax.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants