C# Interview Question #60

What is a multicast delegate in C#?

Exceptions, Delegates, Events & Lambdas Mid-Level Intermediate

Quick Interview Answer

A multicast delegate can reference multiple methods. When invoked, the methods in its invocation list are called in order.

Detailed Explanation

A multicast delegate can reference multiple methods. When invoked, the methods in its invocation list are called in order.

The += operator adds handlers and -= removes them. Events commonly use multicast delegates internally.

For delegates that return values, invoking multiple handlers returns only the result of the last handler, so multicast delegates are most straightforward for void-returning notification scenarios. Exception behavior also needs consideration because an unhandled exception from one handler can interrupt invocation.

Code Example

Action notification = () =>
    Console.WriteLine("Send email");

notification += () =>
    Console.WriteLine("Write log");

notification += () =>
    Console.WriteLine("Update dashboard");

notification();