What is C# multicast delegate?
In C#, a multicast delegate is a type of delegate that can hold references to multiple methods. While regular delegates hold a reference to a single method, multicast delegates can maintain a list of references to multiple methods. When we invoke a multicast delegate, it calls each method in the order they were added. All methods associated with a multicast delegate must have the same signature, meaning they should have the same number and types of parameters. This ensures consistency when the delegate is invoked, and each method in the invocation list receives the same set of arguments.
How C# multicast delegate works
Let’s learn about how multicast delegates work:
Syntax
We use the delegate keyword to declare a multicast delegate, similar to regular delegates, but we can use the += operator to combine multiple methods.
In the case above, all methods added to the MyDelegate multicast delegate must accept a single parameter of type string. Attempting to add a method with a different parameter type or count will result in a compilation error.
Adding methods to delegate
We can add methods with the += operator to add them to the delegate’s invocation list.
In the example above, myDelegate now holds references to Method1, Method2, and Method3.
Invoking the delegate
When we invoke a multicast delegate, it calls each method in the order they were added.
In this case, it will call Method1, Method2, and Method3 with the "Hello, World!" message.
Removing methods
We can remove methods from a multicast delegate using the -= operator.
After this, myDelegate will no longer call Method2.
Delegate demo
Here is a working example of a multicast delegate.
Returning a value with the multicast delegate
When each method associated with a multicast delegate returns a value, it is essential to understand how the values are handled. In C#, a multicast delegate does not automatically aggregate the return values of its methods. Instead, it returns the value of the last method executed. Let’s consider an example below, myDelegate is a multicast delegate that takes an integer parameter and returns an integer. It is composed of two methods, Method1 and Method2. When the delegate is invoked with the argument 5, both methods are executed.
The final result is 10, which is the return value of the last method (Method2) executed. If the methods were invoked in a different order, the result would be based on the return value of the last executed method.
Real-world application of multicast delegates
Multicast delegates are particularly useful when implementing the observer design pattern or event handling in C# because we can dynamically add and remove event handlers. They are commonly used in GUI frameworks and event-driven programming scenarios where multiple methods must respond to an event or action.
Example
Consider a scenario where a mouse click event triggers a multicast delegate. The delegate, in turn, calls various methods with the mouse click coordinates, enabling different methods to respond uniquely. For instance:
In this example, the multicast delegate clickDelegate triggers two methods (HandleClick1 and HandleClick2) in response to a mouse click event, each method performs a specific action based on the coordinates.
Free Resources