Using System;
Namespace SEI. DL88250. Sourcecodes.csharp
{
public delegate void calculation (decimal val1,
Decimal Val2,
ref decimal result);
Class MulticastDelegate
{
Calculation Myadd;
Calculation mysub;
Calculation Mymul;
public void Add (decimal add1, decimal add2, ref decimal result)
{
result = Add1 + add2;
Console.WriteLine ("{0} + {1} = {2}"),
ADD1, ADD2, result);
}
public void Sub (decimal sub1, decimal sub2, ref decimal result)
{
result = SUB1-SUB2;
Console.WriteLine ("{0}-{1} = {2}"),
Sub1, sub2, result);
}
public void Mul (decimal mul1, decimal mul2, ref decimal result)
{
result = Mul1 * MUL2;
Console.WriteLine ("{0} * {1} = {2}"),
MUL1, MUL2, result);
}
static void Main (string[] args)
{
decimal result = 0.0m;
MulticastDelegate mcd = new MulticastDelegate ();
Mcd. Myadd = new Calculation (MCD. ADD);
Mcd. mysub = new Calculation (MCD. SUB);
Mcd. Mymul = new Calculation (MCD. Mul);
Console.foregroundcolor = consolecolor.darkred;
Console.WriteLine ("Use single Delegate:");
Console.foregroundcolor = Consolecolor.darkgray;
Mcd. Myadd (7.43m, 5.19m, ref result);
Mcd. MySub (7.43m, 5.19m, ref result);
Mcd. Mymul (7.43m, 5.19m, ref result);
Console.foregroundcolor = consolecolor.darkred;
Console.WriteLine ("Use multicast Delegate:");
Console.foregroundcolor = Consolecolor.darkgray;
Calculation Multicalc = mcd. Myadd + MCD. MySub + MCD. Mymul;
Multicalc (7.43m, 5.19m, ref result);
Console.foregroundcolor = consolecolor.darkred;
Console.WriteLine ("Remove The Sub Method Delegate:");
Console.foregroundcolor = Consolecolor.darkgray;
Multicalc-= MCD. mysub;
Multicalc (7.43m, 5.19m, ref result);
Restore Delegate Sub method
Multicalc + = mcd. mysub;
Console.foregroundcolor = consolecolor.darkred;
Console.WriteLine ("Delegate contents:");
Console.foregroundcolor = Consolecolor.darkgray;
delegate[] Delegatearray = Multicalc.getinvocationlist ();
foreach (Delegate delgt in Delegatearray)
{
Console.WriteLine (delgt. Method.getbasedefinition ());
}
Console.foregroundcolor = Consolecolor.white;
}
}
}