What are Multicast Delegates in C#?

πŸ’‘ Concept: Multicast Delegate

A multicast delegate in C# is a delegate that can point to and invoke more than one method in a single call. It is commonly used in event handling where multiple subscribers need to respond to the same event.

πŸ“˜ Quick Intro

Multicast delegates allow you to assign multiple methods to a single delegate instance using `+` or `+=` operators. When the delegate is invoked, all the methods in its invocation list are executed sequentially.

🧠 Analogy

Think of a multicast delegate like a group email. When you send a message to the group (delegate), everyone in the group (methods) receives it. All the recipients are notified in the order they were added.

πŸ”§ Technical Explanation

  • πŸ”— A multicast delegate maintains an invocation list of methods to be executed.
  • βž• Use `+=` to add methods and `-=` to remove them.
  • ⏱ All methods are called in the order they were added.
  • 🚫 Return values are ignored except for the last method in the chain.
  • ⚠️ If any method in the chain throws an exception, subsequent methods are not called unless explicitly handled.

🎯 Use Cases

  • βœ… Event notification to multiple subscribers.
  • βœ… Broadcasting messages to several components.
  • βœ… Triggering multiple logging mechanisms.
  • βœ… Performing multiple tasks based on a single action.

πŸ’» Code Example

// Declare delegate
public delegate void Notify();

// Define methods
public class Alerts {
    public void SendSMS() => Console.WriteLine("SMS sent");
    public void SendEmail() => Console.WriteLine("Email sent");
}

public class Program {
    public static void Main() {
        Alerts alert = new Alerts();
        Notify notify = alert.SendSMS;
        notify += alert.SendEmail;

        notify(); // Calls both methods
    }
}

❓ Interview Q&A

Q1: What is a multicast delegate?
A: It’s a delegate that can call multiple methods in a sequence.

Q2: How do you add a method to a multicast delegate?
A: Using `+=` operator.

Q3: Can multicast delegates return multiple values?
A: No, only the return value of the last method is considered.

Q4: Are multicast delegates thread-safe?
A: Not inherently; use locks or concurrent patterns to make them thread-safe.

Q5: What happens if a method in the invocation list throws an exception?
A: It halts execution unless handled.

Q6: How do you remove a method from a multicast delegate?
A: Use `-=` operator.

Q7: Can static methods be used in multicast delegates?
A: Yes, both instance and static methods can be used.

Q8: Can you use anonymous methods with multicast delegates?
A: Yes, and lambdas as well.

Q9: Are events based on multicast delegates?
A: Yes, events use multicast delegates underneath.

Q10: Can delegates be used with async methods?
A: Yes, but invoking them must be handled properly for async execution.

πŸ“ MCQs

Q1. What is a multicast delegate?

  • A sealed delegate
  • A delegate with return value
  • A delegate that can call multiple methods
  • None

Q2. Which operator is used to add methods to a multicast delegate?

  • +
  • ++
  • +=
  • add()

Q3. What does a multicast delegate store?

  • One method
  • Class instances
  • Invocation list of methods
  • Thread references

Q4. What is returned from a multicast delegate?

  • All values
  • First value
  • Return value of the last method
  • Null

Q5. What happens when a method throws exception in a multicast delegate?

  • Continues silently
  • It halts further execution unless handled
  • Skips the method
  • Logs error

Q6. How do you remove a method from a multicast delegate?

  • --
  • -=
  • remove()
  • None

Q7. Are multicast delegates used in events?

  • No
  • Yes
  • Sometimes
  • Only in WinForms

Q8. Which is true about multicast delegate?

  • Only static methods allowed
  • Executes methods in reverse
  • Executes methods in order added
  • No return value

Q9. Which .NET class represents all delegates?

  • System.Object
  • System.Func
  • System.Delegate
  • System.Base

Q10. Can you use multicast delegates with lambda expressions?

  • No
  • Yes
  • Only in .NET 7+
  • Not recommended

πŸ’‘ Bonus Insight

Multicast delegates make it easy to implement observer patterns, where multiple components react to a single event or trigger. They are also great for modular designs where logic can be added or removed without changing the core system.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: