Task<T> (result)
Context: Task<TResult> represents an asynchronous operation that returns a value of type TResult.
using System;using System.Net.Http;using System.Threading.Tasks;
public class Example{ public async Task<string> FetchDataAsync(string url) { using HttpClient client = new HttpClient(); return await client.GetStringAsync(url); }}
// Usage// string data = await new Example().FetchDataAsync("https://api.example.com/data");// Console.WriteLine(data);Accessing the result
Section titled “Accessing the result”- Use
awaitto get the result asynchronously. - Using
.Resultor.Wait()blocks the thread and can cause deadlocks.
Real-world usage example
Section titled “Real-world usage example”Database queries with EF Core: FirstOrDefaultAsync() returns Task<T>, and ToListAsync() returns Task<List<T>>.
Example: EF Core async methods use Task<T> for all database operations.