Examples of multiple delegates (MulticastDelegate) in C #

Source: Internet
Author: User
Tags mul


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;
}
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.