Complete C# Tutorial

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 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! πŸ’‘

Leave a Comment

Share this Doc

Multicast Delegates

Or copy link