Multicast Delegates in C# - Call Multiple Methods Easily!
Introduction
Ever wished you could call multiple methods using a single delegate? Well, good news! Multicast Delegates in C# let you do just that.
Imagine a group WhatsApp message. When you send a message, everyone in the group receives it at the same time. Multicast delegates work the same way! You can assign multiple methods to a delegate, and when you call it, all assigned methods execute one after another.
Sounds cool, right? Letβs dive in! π
π What You Are Going to Learn in This Lesson?
βοΈ What are Multicast Delegates in C#?
βοΈ Why do we need them?
βοΈ How to create and use them?
βοΈ Real-world scenario example
βοΈ Multiple coding examples
βοΈ Step-by-step explanation with output
What Are Multicast Delegates in C#?
A Multicast Delegate is a special type of delegate that can store multiple method references at the same time. When you invoke the delegate, all assigned methods execute sequentially.
π― Key Points:
β
Multicast delegates use +=
to add methods.
β
Use -=
to remove methods.
β
They are useful when you want to trigger multiple actions with a single call.
Syntax of Multicast Delegates in C#
delegate void MyDelegate(); // Declare a delegate
class Program {
static void Method1() {
Console.WriteLine("Method1 executed");
}
static void Method2() {
Console.WriteLine("Method2 executed");
}
static void Main() {
MyDelegate del = Method1; // Assign Method1 to delegate
del += Method2; // Add Method2 to the delegate
del(); // Calling delegate executes both Method1 and Method2
}
}
Simple Example of Multicast Delegates in C#
using System;
delegate void Notify(); // Declare a delegate
class Program {
static void WelcomeMessage() {
Console.WriteLine("Welcome to our program!");
}
static void DisplayDate() {
Console.WriteLine("Today's date: " + DateTime.Now.ToShortDateString());
}
static void Main() {
Notify notify = WelcomeMessage; // Assign first method
notify += DisplayDate; // Add second method
notify(); // Invoke delegate, calls both methods
}
}
π Output:
Welcome to our program!
Today's date: 09/03/2025
π Explanation:
β Notify
delegate stores two methods.
β When notify()
is called, both methods execute in sequence.
β This allows you to trigger multiple actions with just one call! π
Real-World Example: Sending Notifications π©
Imagine you run an online shopping website. When an order is placed, you need to:
β
Send an email confirmation π§
β
Send an SMS notification π±
β
Update the database πΎ
Instead of calling three separate methods, Multicast Delegates in C# make it easy!
using System;
delegate void OrderNotification(); // Declare delegate
class Order {
static void SendEmail() {
Console.WriteLine("π§ Email sent to customer.");
}
static void SendSMS() {
Console.WriteLine("π± SMS notification sent.");
}
static void UpdateDatabase() {
Console.WriteLine("πΎ Order details saved in database.");
}
static void Main() {
OrderNotification notify = SendEmail;
notify += SendSMS;
notify += UpdateDatabase;
notify(); // Invoke delegate, calls all three methods
}
}
π Output:
π§ Email sent to customer.
π± SMS notification sent.
πΎ Order details saved in database.
Why Are Multicast Delegates Important?
β Reduce Code Duplication β No need to call multiple methods separately.
β Improve Code Readability β Methods are added dynamically.
β Better Event Handling β Used in event-driven programming (e.g., GUI events).
β Flexible and Scalable β You can add or remove methods anytime.
Another Example: Logging System π
using System;
delegate void Logger(string message);
class LogSystem {
static void LogToFile(string message) {
Console.WriteLine("π Log saved to file: " + message);
}
static void LogToConsole(string message) {
Console.WriteLine("π₯ Console log: " + message);
}
static void LogToDatabase(string message) {
Console.WriteLine("πΎ Database log: " + message);
}
static void Main() {
Logger log = LogToFile;
log += LogToConsole;
log += LogToDatabase;
log("System started successfully!");
}
}
π Output:
π Log saved to file: System started successfully!
π₯ Console log: System started successfully!
πΎ Database log: System started successfully!
Removing a Method from a Multicast Delegate
Use -=
to remove a method.
log -= LogToDatabase; // Removes database logging
Now, only LogToFile
and LogToConsole
will execute.
π Next What?
Great job! Now you know how Multicast Delegates in C# can call multiple methods with a single delegate. π
π Next, you will learn about Events in C# β a powerful feature that works hand-in-hand with delegates! Stay tuned! π
Β
π― Conclusion
Multicast Delegates in C# make your code cleaner, flexible, and more efficient. Whether youβre sending notifications, handling logs, or processing multiple actions, multicast delegates save time and effort.
Try implementing them in your projects and see how they simplify your code! π‘