Saturday, May 5, 2018

C# : What is multicast delegate in c#

Delegate can invoke only one method reference has been encapsulated into the delegate.it is possible for certain delegate to hold and invoke multiple methods such delegate called multicast delegates.multicast delegates also know as combinable delegates, must satisfy the following conditions:
  • The return type of the delegate must be void. None of the parameters of the delegate type can be delegate type can be declared as output parameters using out keywords.
  • Multicast delegate instance that created by combining two delegates, the invocation list is formed by concatenating the invocation list of two operand of the addition operation. Delegates are invoked in the order they are added.
Implement Multicast Delegates Example:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. delegate void MDelegate();  
  6. class DM {  
  7.     static public void Display() {  
  8.         Console.WriteLine("Meerut");  
  9.     }  
  10.     static public void print() {  
  11.         Console.WriteLine("Roorkee");  
  12.     }  
  13. }  
  14. class MTest {  
  15.     public static void Main() {  
  16.         MDelegate m1 = new MDelegate(DM.Display);  
  17.         MDelegate m2 = new MDelegate(DM.print);  
  18.         MDelegate m3 = m1 + m2;  
  19.         MDelegate m4 = m2 + m1;  
  20.         MDelegate m5 = m3 - m2;  
  21.         m3();  
  22.         m4();  
  23.         m5();  
  24.     }  
  25. }  

No comments:

Post a Comment