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:
3. Add the following under the body tag of Desing page of DelegatesExplained.aspx:
- <form id="form1" runat="server">
- <div>
-
- <asp:Label ID="Label1" runat="server"></asp:Label>
- <asp:Label ID="Label2" runat="server"></asp:Label>
- <asp:Label ID="Label3" runat="server"></asp:Label>
-
- </div>
- </form>
4. Now open .DelegatedExplained.cs and define the delegate name Delegates:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
-
- public delegate void Delegates();
5. Create three functions name First,Second and Third outside the pageload() function:
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- public void First()
- {
- Label1.Text = "First";
- }
-
- public void Second()
- {
- Label2.Text = "Second";
- }
-
- public void Third()
- {
- Label3.Text = "Third";
- }
6. Create Objects Of Delegate under pageload() method:
- protected void Page_Load(object sender, EventArgs e)
- {
- MultipleDelegate m1,m2,m3;
- }
- public void First()
- {
- Label1.Text = "First";
- }
-
- public void Second()
- {
- Label2.Text = "Second";
- }
-
- public void Third()
- {
- Label3.Text = "Third";
- }
7. Now add refrence of Function name First() to m1 and calling the function by calling m1():
- protected void Page_Load(object sender, EventArgs e)
- {
- MultipleDelegate m1;
-
- m1 = First;
- m2=Second; //adding reference to the SecondFunction
- m3=Third; //adding reference to the Third Function
- m1();
- m2();
- m3();
- }
- public void First()
- {
- Label1.Text = "First";
- }
-
- public void Second()
- {
- Label2.Text = "Second";
- }
-
- public void Third()
- {
- Label3.Text = "Third";
- }
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.
- protected void Page_Load(object sender, EventArgs e)
- {
- MultipleDelegate m1, m2, m3,m4;
-
- m1 = First;
-
- m2 = Second;
-
- m3 = Third;
-
- m4 = m1 + m2 + m3;
-
- m4();
- }
- public void First()
- {
- Label1.Text = "First";
- }
-
- public void Second()
- {
- Label2.Text = "Second";
- }
-
- public void Third()
- {
- Label3.Text = "Third";
- }
Now see output will be the same:
You Can also use the "-" operator to remove any object as given below:
Create new object m5:
- protected void Page_Load(object sender, EventArgs e)
- {
- MultipleDelegate m1, m2, m3,m4,m5;
-
- m1 = First;
-
- m2 = Second;
-
- m3 = Third;
-
- m4 = m1 + m2 + m3;
-
- m5 = m4 - m3;
-
- m5();
- }
- public void First()
- {
- Label1.Text = "First";
- }
-
- public void Second()
- {
- Label2.Text = "Second";
- }
-
- public void Third()
- {
- Label3.Text = "Third";
- }
Now output will change and show only two functions: First and Second:
Thanks.