Home > DeveloperSection > Forums > Use of Multicast Delegate in C#?
Anupam Mishra

Total Post:135

Points:949
Posted on    January-27-2016 10:44 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 262  View(s)
Rate this:
Hi Everyone
I am using delegates in program. But situation is to more than one method to adding to a delegate. Please anyone can give me a solution with example how to done this?

Thakyou


Anupam Mishra

Total Post:135

Points:949
Posted on    January-28-2016 3:35 AM

Multicast delegate is an extension of normal delegate. It helps you to point more than one method at a single moment of time.

In a windows application, we created a button on a form and on the button click, we want to invoke two methods (AlertMsg()WarningMsg()). So the code is as follow:


public class Deligatedemo
{
    public Deligatedemo ()
    {
    }
    // Declare a delegate
 public delegate void Notification(string msg);
    public void Process(Notification handler)
    {
        if (handler != null)
            handler("Alert Message");
        if (handler != null)
            handler("Warning Message");
    }

 
public class demoClass
{
    //This method will show alert message to user
    public static void AlertMsg (string s)
    {
         MessageBox.Show(s);
    }

    //This method will show warning message to user
    public static void WarningMsg(string s)
    {
         MessageBox.Show(s);
     }
 } 
private void button1_Click(object sender, System.EventArgs e)
{
     Deligatedemo dc = new Deligatedemo();
    Deligatedemo. Notification notified = null;
    notified+= new DeligateClass. Notification (AlertMsg);
    notified+= new      DeligateClass. Notification (WarningMsg);
    dc.Process(notified);
 }

 


Don't want to miss updates? Please click the below button!

Follow MindStick