C # Multicast Delegate

I study delegates. When i read. I found out that adding multiple functions to a delegate is called a multicast delegate. Based on this, I wrote a program. There are two functions (AddNumbers and MultiplyNumbers) that I added to MyDelegate. Is the following program an example for a multicast delegate ?.

public partial class MainPage : PhoneApplicationPage { public delegate void MyDelegate(int a, int b); // Constructor public MainPage() { InitializeComponent(); MyDelegate myDel = new MyDelegate(AddNumbers); myDel += new MyDelegate(MultiplyNumbers); myDel(10, 20); } public void AddNumbers(int x, int y) { int sum = x + y; MessageBox.Show(sum.ToString()); } public void MultiplyNumbers(int x, int y) { int mul = x * y; MessageBox.Show(mul.ToString()); } } 
+3
source share
3 answers

Yes, this is an example of a multicast delegate. Please note that instead

 new MyDelegate(AddNumbers) 

you can say simply

 AddNumbers 

since there is a so-called method group transformation that will create a delegate instance for you.

One more note: your declaration is public delegate void MyDelegate(int a, int b); should not be inside another type (here inside the MainPage class). It can be a direct member of the namespace (since it is a type). But, of course, this is absolutely true for nesting within a class, just like you, for reasons similar to why you create nested classes.

+7
source

Virtually all C # delegates are MulticastDelegates, even if they only have one method as their target. (Even anonymous functions and lambdas are MulticastDelegates, although they by definition have only one purpose.)

MulticastDelegate is just a base class for all kinds of references to functions or methods in C #, regardless of whether it contains one or more goals.

So this is:

 MyDelegate myDel = new MyDelegate(AddNumbers); 

Sets myDel to MulticastDelegate for one purpose. But this line:

 myDel += new MyDelegate(MultiplyNumbers); 

Upgrading myDel to MulticastDelegate with two goals.

+2
source

Multicast delegates is one of the delegate functions, it wraps a link to several methods and calls it sequentially, and is also known as a delegation chain.

The following is an example of multicast delegates.

  // Declare Delegates public delegate void MultiCast(int num1, int num2); class Program { public void Add(int num1, int num2) { Console.WriteLine(num1 + num2); } public void Sub(int num1, int num2) { Console.WriteLine(num1 - num2); } public void Mul(int num1, int num2) { Console.WriteLine(num1 * num2); } static void Main(string[] args) { MultiCast del1, del2, del3, multAddDel, multSubDel; del1 = new Program().Add; del2 = new Program().Sub; del3 = new Program().Mul; //`There are three ways to define the multicast delegate.` //1 way //Adding delegates multAddDel = del1 + del2 + del3; multAddDel(10, 10); //Removing Delegates multSubDel = multAddDel - del3; multSubDel(10, 10); Console.WriteLine(); Console.WriteLine("Second Way"); //2 way MultiCast multAddDel1 = null; //Adding delegates multAddDel1 += del1; multAddDel1 += del2; multAddDel1 += del3; multAddDel1(10, 10); //Removing Delegates multAddDel1 -= del3; multAddDel1(10, 10); Console.WriteLine(); Console.WriteLine("Third Way"); //3 way MultiCast multAddDel2 = null; //Adding delegates multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del1); multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del2); multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del3); multAddDel2(10, 10); //Removing Delegates multAddDel2 = (MultiCast) Delegate.Remove(multAddDel2, del3); multAddDel2(10, 10); Console.ReadLine(); } } 
0
source

All Articles