Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are multicasting delegates in C#?
A multicasting delegate in C# is a delegate that holds references to multiple methods. When invoked, it calls all the methods in its invocation list sequentially. This is achieved using the += operator to add methods and -= operator to remove methods from the delegate.
Multicasting delegates are particularly useful for implementing event-like behavior where multiple handlers need to be executed when a single event occurs.
Syntax
Following is the syntax for declaring a multicasting delegate −
delegate returnType DelegateName(parameters);
Adding and removing methods from a multicasting delegate −
DelegateName del = Method1; del += Method2; // Add Method2 to invocation list del += Method3; // Add Method3 to invocation list del -= Method1; // Remove Method1 from invocation list
Using Multicasting Delegates
Example
using System;
delegate void myDelegate(int val1, int val2);
public class Demo {
public static void CalAdd(int val1, int val2) {
Console.WriteLine("{0} + {1} = {2}", val1, val2, val1 + val2);
}
public static void CalSub(int val1, int val2) {
Console.WriteLine("{0} - {1} = {2}", val1, val2, val1 - val2);
}
}
public class Program {
static void Main() {
myDelegate d = new myDelegate(Demo.CalAdd);
d += new myDelegate(Demo.CalSub);
Console.WriteLine("Calling both methods:");
d(45, 70);
d -= new myDelegate(Demo.CalAdd);
Console.WriteLine("\nAfter removing CalAdd:");
d(95, 70);
d += new myDelegate(Demo.CalSub);
Console.WriteLine("\nAfter adding CalSub again:");
d(88, 6);
}
}
The output of the above code is −
Calling both methods: 45 + 70 = 115 45 - 70 = -25 After removing CalAdd: 95 - 70 = 25 After adding CalSub again: 88 - 6 = 82 88 - 6 = 82
Simplified Multicasting Syntax
Example
using System;
delegate void Operation(string message);
public class Notifications {
public static void EmailNotification(string message) {
Console.WriteLine("Email: " + message);
}
public static void SMSNotification(string message) {
Console.WriteLine("SMS: " + message);
}
public static void PushNotification(string message) {
Console.WriteLine("Push: " + message);
}
}
public class Program {
static void Main() {
// Simplified syntax without 'new' keyword
Operation notify = Notifications.EmailNotification;
notify += Notifications.SMSNotification;
notify += Notifications.PushNotification;
Console.WriteLine("Sending notifications:");
notify("Order confirmed successfully!");
}
}
The output of the above code is −
Sending notifications: Email: Order confirmed successfully! SMS: Order confirmed successfully! Push: Order confirmed successfully!
Key Rules
-
Methods are executed in the order they were added to the delegate.
-
If a method throws an exception, subsequent methods in the invocation list won't be executed.
-
For delegates with return values, only the last method's return value is returned.
-
Removing a method that doesn't exist in the invocation list has no effect.
Conclusion
Multicasting delegates in C# allow a single delegate to reference and invoke multiple methods sequentially. They are essential for implementing event handling patterns and scenarios where multiple actions need to be performed in response to a single trigger, using the += and -= operators for managing the method invocation list.
