C # Lambda Expressions

Lambda expressions

"Lambda Expressions" is an anonymous function, is a highly functional programming similar expressions, Lambda simplify the amount of code developers need to write. It can contain expressions and statements, and can be used to create a delegate or expression tree type, with support inline expressions can be bound to the delegate or expression tree of input parameters.

Either of these expressions

1. Expression its body: (input-parameters) => expression
2. Block as its subject:(input-parameters) => { <sequence-of-statements> }

Expressions Lambda:

Expression positioned => lambda expression to the right of the operator as "lambda expressions." Lambda expression returns the result of the expression, and the following basic forms:

(input parameters) => expression

Only when lambda is only one input parameter, brackets is optional; otherwise brackets are required. Two or more brackets in the input parameters using commas to be separated:

(x, y) => x == y

Sometimes, the compiler is difficult or impossible to infer the input types. If this happens, you can explicitly specify the type described by the following example:

(int x, string s) => s.Length > x

Use empty parentheses specify zero input parameters:

() => SomeMethod()

Lambda statement

When lambda expressions, a plurality of statements written in the form:

(input parameters) => {statement;}

E.g:

delegate void TestDelegate(string s);
…
TestDelegate myDel = n => { string s = n + " " + "World"; Console.WriteLine(s); };
myDel("Hello");

Guess you like

Origin www.cnblogs.com/halfsaltedfish/p/11374224.html