← Back to C# Course | Chapter 12: Delegates & Events | Lesson 2 of 6

Multicast Delegates क्या हैं

एक multicast delegate एक साथ कई methods रखता है, जैसे एक group chat message जो सबको पहुंचता है। जब इसे call किया जाता है, वे सभी एक के बाद एक चलते हैं।

In this page:

  1. Multicast Delegates
Syntax
csharp
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

markup
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");
    }
}
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. किसी multicast delegate में हर method की return value की उम्मीद करना, जबकि सिर्फ आखिरी वाली return होती है।
  2. -= से एक ऐसा lambda हटाने की कोशिश करना जो जोड़े गए से अलग instance है।
  3. यह भूल जाना कि एक method के throw करने से बाकी रुक जाते हैं।
🔒

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.