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