How To Implement Multicast Delegates In C#

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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. delegate void MDelegate();  
  6. class DM  
  7. {  
  8.     static public void Display()  
  9.     {  
  10.         Console.WriteLine("Meerut");  
  11.     }  
  12.     static public void print()  
  13.     {  
  14.         Console.WriteLine("Roorkee");  
  15.     }  
  16. }  
  17. class MTest  
  18. {  
  19.     public static void Main()  
  20.     {  
  21.         MDelegate m1 = new MDelegate(DM.Display);  
  22.         MDelegate m2 = new MDelegate(DM.print);  
  23.         MDelegate m3 = m1 + m2;  
  24.         MDelegate m4 = m2 + m1;  
  25.         MDelegate m5 = m3 - m2;  
  26.         m3();  
  27.         m4();  
  28.         m5();  
  29.     }  
  30. }  
Output of the porgram
 
 
 
Thank you. If you have any suggestion and comment about my blog, pleaze let me know.
Ebook Download
View all
Learn
View all