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

Task<T> क्या है

एक Task of T एक वादा है जो एक gift के साथ आता है: background job खत्म होने पर, आपको T type का एक जवाब मिलता है। यह खाना order करने और तैयार होने पर meal पाने जैसा है।

In this page:

  1. Task<T>
Syntax
csharp
static async Task<type> MethodNameAsync()
{
    await Task.Delay(milliseconds);
    return value;
}

type result = await MethodNameAsync();

Task<T>

Task<T> एक asynchronous operation है जो आखिरकार T type का एक result produce करता है। इसे await करना task पूरा होते ही आपको सीधे T value देता है। यह एक non-void return type वाले method का asynchronous equivalent है।

Note: Task<T> पर .Result property synchronously block करती है — deadlocks से बचने के लिए await को प्राथमिकता दें।

उदाहरण: 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);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. task पूरा होने से पहले .Result पढ़ना, जो thread को block करता है।
  2. यह भूल जाना कि Task<T> return करने वाला एक async method T return करता है, Task<T> नहीं।
  3. awaited task से exceptions को handle न करना।
🔒

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.