C# > Advanced C# > Delegates and Events > Delegates Declaration and Usage
Multicast Delegates
This code illustrates the use of multicast delegates, which allow a single delegate instance to hold a reference to multiple methods. When the delegate is invoked, all the methods in its invocation list are executed in the order they were added. This is useful for scenarios where you need to perform multiple actions sequentially.
Delegate Declaration
This declares a delegate type named `MessageHandler`. It can reference methods that take a string as input and return void (perform an action).
public delegate void MessageHandler(string message);
Method Implementations
These are two methods, `LogToConsole` and `LogToFile`, that match the `MessageHandler` delegate's signature. They represent different logging actions that we want to perform.
public static void LogToConsole(string message)
{
Console.WriteLine("Console: " + message);
}
public static void LogToFile(string message)
{
//Simulate writing to a file
Console.WriteLine("File: " + message);
}
Multicast Delegate Instantiation and Invocation
First, we create a `MessageHandler` instance and assign `LogToConsole` to it. Then, we use the `+=` operator to add `LogToFile` to the delegate's invocation list. When `logger` is invoked with a message, both `LogToConsole` and `LogToFile` will be executed, one after the other. The `-=` operator can be used to remove methods from the invocation list.
MessageHandler logger = LogToConsole;
logger += LogToFile; // Add LogToFile to the invocation list
logger("This is a log message.");
Complete Code Example
This is the complete, runnable code example, showing the delegate declaration, method implementations, and multicast delegate instantiation and invocation. When you run this code, you'll see output from both `LogToConsole` and `LogToFile`.
using System;
public delegate void MessageHandler(string message);
public class MulticastDelegateExample
{
public static void LogToConsole(string message)
{
Console.WriteLine("Console: " + message);
}
public static void LogToFile(string message)
{
//Simulate writing to a file
Console.WriteLine("File: " + message);
}
public static void Main(string[] args)
{
MessageHandler logger = LogToConsole;
logger += LogToFile;
logger("This is a log message.");
}
}
Concepts Behind the Snippet
Multicast delegates leverage the `+=` and `-=` operators to manage a list of methods to be executed. This allows for a flexible and dynamic way to perform multiple actions in response to a single event or trigger. The order of execution is the order in which the methods were added to the invocation list.
Real-Life Use Case
Multicast delegates are commonly used in event handling. When an event is raised, multiple event handlers (methods) might be subscribed to that event. The event's underlying multicast delegate ensures that all subscribed handlers are executed.
Best Practices
Interview Tip
Know that multicast delegates execute synchronously, meaning each method in the invocation list is executed one after another in sequence. Consider asynchronous alternatives (like `async` and `await`) if you need to perform these actions concurrently to avoid blocking the main thread.
When to Use Them
Use multicast delegates when you need to execute multiple actions in response to a single event or trigger, such as logging to multiple destinations, notifying multiple subscribers, or performing a series of operations sequentially.
Memory Footprint
The memory footprint of a multicast delegate is slightly larger than a single-cast delegate because it needs to maintain a list of method references. The more methods added to the invocation list, the larger the memory footprint.
Alternatives
Alternatives to multicast delegates include using lists of actions (e.g., `List
Pros
Cons
FAQ
-
What happens if one of the methods in a multicast delegate throws an exception?
If a method in the invocation list throws an exception, the execution of the remaining methods might be interrupted. It's crucial to handle exceptions within each method to prevent this from happening. -
How can I remove a method from a multicast delegate's invocation list?
You can use the `-=` operator to remove a method from the invocation list. For example: `logger -= LogToFile;`