Introduction
In this article, we will see how to implement Multicast Delegate in C#. Delegate can invoke only one method refrence, which has been encapsulated into the Delegate. It is possible for certain Delegate to hold and invoke multiple methods such Delegates are called Multicast Delegates. Multicast Delegates are also known as Combinable Delegates, which must satisfy the conditions like the return type of the Delegate must be void. None of the parameters of the Delegate type can be Delegate type, which can be declared as the output parameters, using out keywords. Multicast Delegate instance is created by combining two Delegates and the invocation list is formed by concatenating the invocation list of the two operands of the addition operation. Delegates are invoked in the order they are added.
Implement Multicast Delegates eExample
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- delegate void MDelegate();
- class DM
- {
- static public void Display()
- {
- Console.WriteLine("Meerut");
- }
- static public void print()
- {
- Console.WriteLine("Roorkee");
- }
- }
- class MTest
- {
- public static void Main()
- {
- MDelegate m1 = new MDelegate(DM.Display);
- MDelegate m2 = new MDelegate(DM.print);
- MDelegate m3 = m1 + m2;
- MDelegate m4 = m2 + m1;
- MDelegate m5 = m3 - m2;
- m3();
- m4();
- m5();
- }
- }
Output of the porgram
Thank you. If you have any suggestion and comment about my blog, pleaze let me know.