Simple and Multicast Delegate in C#

A useful property of delegate objects is that multiple objects can be assigned to one delegate instance by using the + operator. The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.

The "-" operator can be used to remove a component delegate from a multicast delegate.

1.
Open Visual Studio go to>>File>>New>>Website>>Asp.net Empty Website and name anything but named DelegatesExplained:

 
2. Add New WebForm to your Project and Name DelegateExplained.aspx,you can name anything:

code

3.
Add the following under the body tag of Desing page of DelegatesExplained.aspx:
  1. <form id="form1" runat="server">  
  2.     <div>  
  3.       
  4.     <asp:Label ID="Label1" runat="server"></asp:Label>  
  5.     <asp:Label ID="Label2" runat="server"></asp:Label>  
  6.     <asp:Label ID="Label3" runat="server"></asp:Label>  
  7.   
  8.     </div>  
  9.     </form>  
4. Now open .DelegatedExplained.cs and define the delegate name Delegates:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8.   
  9. public delegate void Delegates();  
5. Create three functions name First,Second and Third outside the pageload() function:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.   
  4. }  
  5. public void First()  
  6. {  
  7.     Label1.Text = "First";  
  8. }  
  9.   
  10. public void Second()  
  11. {  
  12.     Label2.Text = "Second";  
  13. }  
  14.   
  15. public void Third()  
  16. {  
  17.     Label3.Text = "Third";  
  18. }  
6. Create Objects Of Delegate under pageload() method:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     MultipleDelegate m1,m2,m3;  
  4. }  
  5. public void First()  
  6. {  
  7.     Label1.Text = "First";  
  8. }  
  9.   
  10. public void Second()  
  11. {  
  12.     Label2.Text = "Second";  
  13. }  
  14.   
  15. public void Third()  
  16. {  
  17.     Label3.Text = "Third";  
  18. }  
 7. Now add refrence of Function name First() to m1 and calling the function by calling m1():
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     MultipleDelegate m1;  
  4.   
  5.     m1 = First;  //adding reference to the First Function  
  6.     m2=Second;   //adding reference to the SecondFunction
  7.     m3=Third;  //adding reference to the Third Function
  8.     m1();
  9.     m2();
  10.     m3();
  11. }  
  12. public void First()  
  13. {  
  14.     Label1.Text = "First";  
  15. }  
  16.   
  17. public void Second()  
  18. {  
  19.     Label2.Text = "Second";  
  20. }  
  21.   
  22. public void Third()  
  23. {  
  24.     Label3.Text = "Third";  
  25. }  
Now run by pressing ctrl+f5;
 
 Now see output will be:
 
 

Now Multicast Delegates by using + operator  rather then calling each object separately create a new object m4 and add all the delegates objects in it.
 
 Only delegates of the same type can be combined.
  1.    protected void Page_Load(object sender, EventArgs e)  
  2.    {  
  3.        MultipleDelegate m1, m2, m3,m4;  
  4.          
  5.        m1 = First;  //adding reference to the First Function  
  6.   
  7.        m2 = Second;   //adding reference to the SecondFunction  
  8.   
  9.        m3 = Third;  //adding reference to the Third Function  
  10.   
  11.        m4 = m1 + m2 + m3;   //casting all three objects of MuticastDelegate to one object;  
  12.   
  13.        m4();  
  14.    }  
  15.    public void First()  
  16.    {  
  17.        Label1.Text = "First";  
  18.    }  
  19.   
  20.    public void Second()  
  21.    {  
  22.        Label2.Text = "Second";  
  23.    }  
  24.   
  25.    public void Third()  
  26.    {  
  27.        Label3.Text = "Third";  
  28.    }  
 Now see output will be the same:
 
 
You Can also use the "-" operator to remove any object as given below:
Create new object m5:
  1.    protected void Page_Load(object sender, EventArgs e)  
  2.    {  
  3.        MultipleDelegate m1, m2, m3,m4,m5;  
  4.          
  5.        m1 = First;  //adding reference to the First Function  
  6.   
  7.        m2 = Second;   //adding reference to the SecondFunction  
  8.   
  9.        m3 = Third;  //adding reference to the Third Function  
  10.   
  11.        m4 = m1 + m2 + m3;   //casting all three objects of MuticastDelegate to one object;  
  12.   
  13.        m5 = m4 - m3;  
  14.   
  15.        m5();  
  16.    }  
  17.    public void First()  
  18.    {  
  19.        Label1.Text = "First";  
  20.    }  
  21.   
  22.    public void Second()  
  23.    {  
  24.        Label2.Text = "Second";  
  25.    }  
  26.   
  27.    public void Third()  
  28.    {  
  29.        Label3.Text = "Third";  
  30.    }  
Now output will change and show only two functions: First and Second:
 
 
Thanks.