A lambda expression in C # is the evolution of an anonymous method in C # 2, so you start by simplifying the way you do it from an anonymous function.
The example is to get a string parameter and then return an int.
First, the anonymous method is written as follows:
Func<string, int> returnlength; Returnlength = Delegate (string text) {return text. Length; }; Console.Write (Returnlength ("Hello"));
The most verbose form of a lambda expression is:
(Explicit type argument list) = = {statement} (= = can be seen as goes to)
The above example LANBDA expression can be written as:
Func<string, int> returnlength; Returnlength = (string text) = = {return text. Length; }; Console.Write (Returnlength ("Hello")); Console.read ();
This notation is very similar to the anonymous method
In addition to returning a value in the form of a block of code, most of the time an expression is used to represent the entire body, and the value of the expression is the result of the lambda, and the format becomes:
(explicit argument list) = = Expression
The above example becomes
Func<string, int> returnlength; Returnlength = (string text) = = text. Length;
Because the compiler is strong enough to be able to easily guess the type of the parameter, in this case it can be further simplified to:
(Implicitly typed argument list) = = Expression
The above example becomes:
Func<string, int> returnlength; Returnlength = (Text) = = text. Length;
So far it's quite brief, but in the case of a parameter, this parenthesis is a bit of an eyesore, so you can continue to simplify:
Parameter name-= Expression
The final form of the lambda expression for an example is:
Func<string, int> returnlength; Returnlength = text + text. Length;
Lambda Expression Learning Note (1)-a lambda expression as a delegate