What is a multicast delegate in C#?

Category: C SharpWhat is a multicast delegate in C#?
Editor">Editor Staff asked 2 years ago

What is a multicast delegate in C#?

1 Answers
Editor">Editor Staff answered 2 years ago

Delegate is one of the base types in .NET. Delegate is a class that is used to create and invoke delegates at runtime.
 
A delegate in C# allows developers to treat methods as objects and invoke them from their code.
 
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. }