Learning from C # Delegation

Source: Internet
Author: User

delegate int getcalculatedvaluedelegate (int x, int y); A definition is a delegate that is actually an abstract class of parameter list form and return value of the same function addcalculator,subcalculator both the parameter form of the function and the type of return value is.

static int Addcalculator (int x, int y)
{
return x + y;
}

static int Subcalculator (int x, int y)
{
return x-y;
}

static int Calculator (Getcalculatedvaluedelegate del, int x, int y)//define a function to pass in three parameters the type of the first parameter is a delegate, which means that the delegate is a type.
{
Return del (x, y);
}

static void Main (string[] args)
{
Console.WriteLine (Calculator (Addcalculator, 10, 20));
}

In the example above, the first parameter of the calculator function is a delegate. In fact, what calculator does with X and Y, it doesn't know how to handle X and Y is determined by getcalculatedvaluedelegate. So in the main method, we pass the Addcalculator method as a parameter to calculator, which means that Calculator uses addcalculator logic to process X and Y. This is also very image: Calculator said: "I do not know how to deal with X and Y, let del to deal with it!" So I threw X and Y to Del.

Application of Lamada expression

n=>n%2==0;

Equivalent to

delegate (int n) {return n%2==0;}

Learning from C # Delegation

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.