26.1 lambda expressions
C #2.0 introduces anonymous methods, which allow code blocks to be written "in-line" where Del Egate values are expected. while anonymous methods provide much of the expressive power of functional programming languages, the anonymous method syntax is rather verbose and imperative in nature. lambda expressions provide a more concise, functional syntax for writing anonymous methods.
C #2.0 introduces an anonymous function, which allows the code block to be written as "inline" where the proxy value is expected. While anonymous functions provide the power of functional programming languages, the markup of anonymous functions is also quite lengthy. Lambda expressions provide more concise functional tags for writing anonymous functions.
A Lambda expression is written as a parameter list, followed by the => token, followed by an expression or a statement block.
Lambda expressions are written as a list of parameters followed by a => tag, followed by an expression or a block.
Expression:
Assignment
Non-assignment-expression
Non-assignment-expression:
Conditional-expression
Lambda-expression
Query-expression
Lambda-expression:
(Lambda-parameter-listopt) => lambda-expression-body
Implicitly-typed-lambda-parameter => lambda-expression-body
Lambda-parameter-list:
Explicitly-typed-lambda-parameter-list
Implicitly-typed-lambda-parameter-list
Explicitly-typed-lambda-parameter-list
Explicitly-typed-lambda-parameter
Explicitly-typed-lambda-parameter-list, explicitly-typed-lambda-parameter
Explicitly-typed-lambda-parameter:
Parameter-modifieropt type identifier
Implicitly-typed-lambda-parameter-list
Implicitly-typed-lambda-parameter
Implicitly-typed-Lambda-parameter-list, implicitly-typed-Lambda-Parameter
Implicitly-typed-Lambda-parameter:
Identifier
Lambda-expression-body:
Expression
Block
The parameters of a Lambda expression can be explicitly or implicitly typed. in an explicitly typed parameter list, the type of each parameter is explicitly stated. in an implicitly typed parameter list, the types of the parameters are inferred from the context in which the lambda expression occurs-specifically, when the lambda expression is converted to a compatible delegate type, that delegate type provides the parameter types (§ 26. 3.1 ).
Lambda expressions can be explicit or implicit. In an explicit type parameter list, each parameter type must be explicitly declared. In an implicit type parameter list, the parameter type is inferred from the context when a Lambda expression is generated. When a Lambda expression is converted to a matching proxy type, that is, the type of the parameter provided by the proxy type.
In a lambda expression with a single, implicitly typed parameter, the parentheses may be omitted from the parameter list. In other words, a lambda expression of the form
In a lambda expression with unique and explicit parameters, parentheses can be deleted from the parameter list. In other words, a lambda expression of this type
(Param) => expr
Can be abbreviated
Can be abbreviated
Param => expr
Some examples of lambda expressions follow below:
Examples of some lambda expressions are as follows:
X => x + 1 // Implicitly typed, expression body
X => {return x + 1;} // Implicitly typed, statement body
(Int x) => x + 1 // Explicitly typed, expression body
(Int x) =>{ return x + 1;} // explicitly typed, statement body
(X, y) => X * y // multiple parameters
() => Console. writeline () // No Parameters
In general, the specification of anonymous methods, provided in § 21 of the C #2.0 specification, also applies to lambda expressions. lambda expressions are a functional superset of anonymous methods, providing the following additional functionality:
Generally, the anonymous Function Specification, provided in section 21 in C #2.0, is also applicable to lambda expressions. Lambda expressions are supersets of anonymous functions. They provide the following additional functions:
· Lambda expressions permit parameter types to be omitted and inferred whereas anonymous methods require parameter types to be explicitly stated.
· Lambda expressions allow deletion and inference of parameter types, while anonymous functions require explicit declaration of parameter types.
· The body of a lambda expression can be an expression or a statement block whereas the body of an anonymous method can only be a statement block.
· The body of a Lambda expression can be an expression or a declaration block, while that of an anonymous function can only be a declaration block.
· Lambda expressions passed as arguments participant ipate in type argument inference (§ 26. 3.2) and in method overload resolution (§ 26. 3.3 ).
· The Lambda expression is passed as the inference of the parameter involved in the type parameter (§ 26. 3.2) and the argument is overloaded in the function (§ 26. 3.3 ).
· Lambda expressions with an expression body can be converted to expression trees (§ 26. 8 ).
· A Lambda expression has an expression subject that can be converted to an Expression Tree (§ 26. 8 ).
NoteNote:
The PDC 2005 Technology preview compiler does not support lambda expressions with a statement block body. In cases where a statement block body is needed, the C #2.0 anonymous method syntax must be used.
The PDC 2005 Technology preview compiler does not support lambda expressions containing declarative block bodies. Once the block body is declared, the anonymous function mark of C #2.0 must be used.