Task<T> क्या है
एक Task of T एक वादा है जो एक gift के साथ आता है: background job खत्म होने पर, आपको T type का एक जवाब मिलता है। यह खाना order करने और तैयार होने पर meal पाने जैसा है।
In this page:
Syntax
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>
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
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. #}
आम गलतियां
- task पूरा होने से पहले
.Resultपढ़ना, जो thread को block करता है। - यह भूल जाना कि
Task<T>return करने वाला एकasyncmethodTreturn करता है,Task<T>नहीं। - awaited task से exceptions को handle न करना।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: