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. <asp:Label ID="Label1" runat="server"></asp:Label>
  4. <asp:Label ID="Label2" runat="server"></asp:Label>
  5. <asp:Label ID="Label3" runat="server"></asp:Label>
  6. </div>
  7. </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. 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. public void First()
  5. {
  6. Label1.Text = "First";
  7. }
  8. public void Second()
  9. {
  10. Label2.Text = "Second";
  11. }
  12. public void Third()
  13. {
  14. Label3.Text = "Third";
  15. }
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. public void Second()
  10. {
  11. Label2.Text = "Second";
  12. }
  13. public void Third()
  14. {
  15. Label3.Text = "Third";
  16. }
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. m1 = First; //adding reference to the First Function
  5. m2=Second; //adding reference to the SecondFunction
  6. m3=Third; //adding reference to the Third Function
  7. m1();
  8. m2();
  9. m3();
  10. }
  11. public void First()
  12. {
  13. Label1.Text = "First";
  14. }
  15. public void Second()
  16. {
  17. Label2.Text = "Second";
  18. }
  19. public void Third()
  20. {
  21. Label3.Text = "Third";
  22. }
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. m1 = First; //adding reference to the First Function
  5. m2 = Second; //adding reference to the SecondFunction
  6. m3 = Third; //adding reference to the Third Function
  7. m4 = m1 + m2 + m3; //casting all three objects of MuticastDelegate to one object;
  8. m4();
  9. }
  10. public void First()
  11. {
  12. Label1.Text = "First";
  13. }
  14. public void Second()
  15. {
  16. Label2.Text = "Second";
  17. }
  18. public void Third()
  19. {
  20. Label3.Text = "Third";
  21. }
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. m1 = First; //adding reference to the First Function
  5. m2 = Second; //adding reference to the SecondFunction
  6. m3 = Third; //adding reference to the Third Function
  7. m4 = m1 + m2 + m3; //casting all three objects of MuticastDelegate to one object;
  8. m5 = m4 - m3;
  9. m5();
  10. }
  11. public void First()
  12. {
  13. Label1.Text = "First";
  14. }
  15. public void Second()
  16. {
  17. Label2.Text = "Second";
  18. }
  19. public void Third()
  20. {
  21. Label3.Text = "Third";
  22. }
Now output will change and show only two functions: First and Second:
Thanks.