Lambda expressions, lambda expressions
1. A Lambda expression is an anonymous function. Its essence is actually a function. Therefore, any Lambda expression can be implemented or replaced by a common function in other ways.
2. Lambda expression cloud OPERATOR: =>, which is read as "goes ".
3. Lambda Syntax: parameter list => function body. There are some differences in the form according to the usage, as shown below:
1>. If there is only one parameter, "()" in the parameter list can be omitted. If there is no or more parameters, no;
Num => Console. Write ("the brackets in the parameter list can be omitted when one parameter is set. Value: {0}", num );
() => Console. Write ("0 Parameters ");
(X, y) => Console. Write ("contains two parameters, value: {0}, {1}", x, y );
2> when a Lambda expression is directly used as a delegate implementation method, the type before the parameters in the form parameter list can sometimes be omitted. The ref and out modifiers must correspond to the delegate;
3>. If there is only one statement in the function body, the braces can be omitted; otherwise, no;
Num => Console. Write ("only one statement ");
Num => {num + = 1; Console. Write ("two or more statements must have braces ");};
4> when the function body has a return value, if only one statement can omit "return", otherwise it cannot.
4. Func <...> and Action <...>
Both are delegates in the System space in the. net class library. Each delegate can support up to 16 parameters;
Among them, Func <...> is the delegate with the returned value, and Action <...> is the delegate without the returned value;
. In the. net class library and when the delegate is used as a parameter or variable during normal development, Func can be used for 16 parameters or below <...> or Action <...>.
5. Linq
At the beginning, Lambda expressions are always confused with Linq (horizontal comparison of Low );
The Language Integrated Query (Linq) is a set of extensions used for the C # Language. It allows you to write C # code to Query memory data in the same way as a database;
The parameters of the Select, OrderBy, GroupBy, Where, Min, Max, and other methods of Linq are basically Func <...> type delegation. You can use Lambda expressions for definition in specific use.