Task<T>
In this page:
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>
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);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: