๐ŸŽ“ A1Learner
๐Ÿ”
0 of 38 lessons completed

C# Async Methods

An async method can return a value wrapped in a generic Task. Awaiting that Task gives the value itself.

One Fetch, One Value

Fetch waits and then returns a data string. Main awaits the task and prints the string.

using System;
using System.Threading.Tasks;

class Program
{
    static async Task<string> Fetch(string name)
    {
        await Task.Delay(10);
        return "Data for " + name;
    }

    static async Task Main(string[] args)
    {
        string result = await Fetch("solo");
        Console.WriteLine(result);
    }
}

Two Fetches in Sequence

Main awaits one fetch before it starts the next one. The values print in call order.

using System;
using System.Threading.Tasks;

class Program
{
    static async Task<string> Fetch(string name)
    {
        await Task.Delay(10);
        return "Data for " + name;
    }

    static async Task Main(string[] args)
    {
        string first = await Fetch("north");
        string second = await Fetch("south");
        Console.WriteLine(first);
        Console.WriteLine(second);
    }
}

Three Results From a Group Wait

Three fetches start together and the group wait collects their values. The results print in the order the tasks were listed.

using System;
using System.Threading.Tasks;

class Program
{
    static async Task<string> Fetch(string name)
    {
        await Task.Delay(10);
        return "Data for " + name;
    }

    static async Task Main(string[] args)
    {
        Task<string> first = Fetch("one");
        Task<string> second = Fetch("two");
        Task<string> third = Fetch("three");
        string[] results = await Task.WhenAll(first, second, third);
        Console.WriteLine(results[0]);
        Console.WriteLine(results[1]);
        Console.WriteLine(results[2]);
    }
}

Example

using System;
using System.Threading.Tasks;

class Program
{
    static async Task<string> Fetch(string name, int delay)
    {
        await Task.Delay(delay);
        return "Data for " + name;
    }

    static async Task Main(string[] args)
    {
        Task<string> first = Fetch("alpha", 30);
        Task<string> second = Fetch("beta", 10);
        string[] results = await Task.WhenAll(first, second);
        Console.WriteLine(results[0]);
        Console.WriteLine(results[1]);
    }
}

Key points

  • Task<string> is a Task that carries a string result.
  • The type inside the angle brackets names the result type.
  • An async method with return type Task<string> returns its string with a return statement.
  • Awaiting a Task<string> gives a string value.
  • A call to Fetch starts the fetch and gives back its Task.
  • Task.WhenAll over value tasks returns an array of their results.
  • That result array follows the order of the tasks, not the order they finish in.

In this example, Main starts the alpha fetch first and the beta fetch second. The beta fetch has the shorter wait, so it finishes first. Main awaits Task.WhenAll for both tasks. The results array keeps task order, so the alpha line prints before the beta line.

Note

A shorter wait can finish first and still sit later in the result array.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Fetches start in the order alpha, then beta, and beta finishes first. What does results[0] hold after the group wait?