Multicast Delegates क्या हैं
एक multicast delegate एक साथ कई methods रखता है, जैसे एक group chat message जो सबको पहुंचता है। जब इसे call किया जाता है, वे सभी एक के बाद एक चलते हैं।
In this page:
Syntax
delegate void DelegateName(type parameter);
DelegateName handlers = MethodOne;
handlers += MethodTwo; // add another method
handlers -= MethodOne; // remove one
handlers(argument); // calls all of them
Multicast Delegates
एक delegate += से उन्हें combine करके एक साथ एक से ज़्यादा methods को refer कर सकता है, एक multicast delegate बनाते हुए।
इसे invoke करना हर attached method को order में call करता है। -= invocation list से एक method हटाता है।
यही वह mechanism है जो events पीछे उपयोग करते हैं।
Note:
अगर किसी multicast delegate का return type non-void हो, इसे invoke करना आपको सिर्फ आखिरी method की return value देता है।
उदाहरण: Multicast Delegates
using System;
delegate void Notify(string message);
class Program
{
static void LogToConsole(string msg) => Console.WriteLine("Console: " + msg);
static void LogWithPrefix(string msg) => Console.WriteLine("[LOG] " + msg);
static void Main(string[] args)
{
Notify notify = LogToConsole;
notify += LogWithPrefix;
notify("System started");
}
}
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. #}
आम गलतियां
- किसी multicast delegate में हर method की return value की उम्मीद करना, जबकि सिर्फ आखिरी वाली return होती है।
-=से एक ऐसा lambda हटाने की कोशिश करना जो जोड़े गए से अलग instance है।- यह भूल जाना कि एक method के throw करने से बाकी रुक जाते हैं।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: