← Back to C# Course | Chapter 13: Async Programming | Lesson 3 of 6

Task<T>

In this page:

  1. Task<T>

Task<T>

Task<T> is an asynchronous operation that eventually produces a result of type T. Awaiting it gives you the T value directly once the task completes. It's the asynchronous equivalent of a method with a non-void return type.

Note: The .Result property on Task<T> blocks synchronously — prefer await to avoid deadlocks.

Example: Task<T>

markup
using System;
using System.Threading.Tasks;

class Program
{
    static async Task<int> SumAsync(int a, int b)
    {
        await Task.Delay(50);
        return a + b;
    }

    static async Task Main(string[] args)
    {
        int result = await SumAsync(4, 5);
        Console.WriteLine(result);
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.