.NET - Get started with and await


The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any. When the await operator is applied to the operand that represents already completed operation, it returns the result of the operation immediately without suspension of the enclosing method. The await operator doesn’t block the thread that evaluates the async method. When the await operator suspends the enclosing async method, the control returns to the caller of the method.

using System;
using System.Threading.Tasks;
using System.Diagnostics;

namespace AwaitExam
    class Program
        static async Task<int> GetValue(int i)
            var sw = new Stopwatch();
            Console.WriteLine("GetValue({0}): Starting", i);
            Task wait = Task.Delay(i * 1000);
            Console.WriteLine("GetValue({0}): Begin await at {1:0.00} seconds", i, sw.Elapsed.TotalSeconds);
            await wait;
            Console.WriteLine("GetValue({0}): End await at {1:0.00} seconds", i, sw.Elapsed.TotalSeconds);
            return i;

        static async Task DoWork()
            var t1 = GetValue(2);
            var t2 = GetValue(3);
            Console.WriteLine("Return: {0}", await t1);
            Console.WriteLine("Return: {0}", await t2);

        static void Main(string[] args)


GetValue(2): Starting
GetValue(2): Begin await at 0.01 seconds
GetValue(3): Starting
GetValue(3): Begin await at 0.00 seconds
GetValue(2): End await at 2.01 seconds
Return: 2
GetValue(3): End await at 3.00 seconds
Return: 3