现在的位置: 首页 > 综合 > 正文

C#3.0规范(四)Lambda 表达式

2013年02月07日 ⁄ 综合 ⁄ 共 3772字 ⁄ 字号 评论关闭
 

26.1 Lambda expressions Lambda 表达式

C# 2.0 introduces anonymous methods, which allow code blocks to be written “in-line” where delegate 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 引入了匿名函数,它允许代码块能够被写成“内联”在代理值所期望的地方。当匿名函数提供功能性编程语言的巨大威力的同时,匿名函数的标记也显得相当的冗长。Lambda表达式提供了更简明的功能性标记来书写匿名函数。

A lambda expression is written as a parameter list, followed by the => token, followed by an expression or a statement block.

Lambda表达式书写为一组参数列表,紧接着=>标记,然后跟随某个表达式或声明块。

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表达式的参数可以是显式的或者隐式的类型。在一个显式类型参数列表中,每个参数的类型都必须显式声明。在一个隐式类型参数列表中,参数类型是根据lambda表达式产生时的上下文环境推断出来的,当一个lambda表达式被转化为一个匹配的代理类型,也就是那个代理类型提供参数的类型。

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

在一个具有唯一的,显式类型参数的lambda表达式中,圆括号可以从参数列表中删除。换句话说,一个这种类型的lambda表达式

( param ) => expr

can be abbreviated to

可以被缩写为

param => expr

Some examples of lambda expressions follow below:

一些lambda表达式的例子如下所示:

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:

通常,匿名函数规范,C#2.0规范中§21提供的,也适用于lambda表达式。Lambda表达式是匿名函数功能性的超集,提供了下列额外的功能:

·         Lambda expressions permit parameter types to be omitted and inferred whereas anonymous methods require parameter types to be explicitly stated.

·         Lambda表达式允许参数类型被删除和推断,而匿名函数需要参数类型的显式声明。

·         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.

·         Lambda表达式的主体可以是一个表达式或者是一个声明块,而匿名函数的主体只能是声明块。

·         Lambda expressions passed as arguments participate in type argument inference (§26.3.2) and in method overload resolution (§26.3.3).

·         Lambda表达式传递为参数参与类型参数的推论26.3.2)并且在函数中重载论断26.3.3)

·         Lambda expressions with an expression body can be converted to expression trees (§26.8).

·         Lambda表达式具有一个表达式主体能够被转化为表达式树26.8)

Note注意

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.

PDC 2005技术预览编译器不支持lambda表达式含有声明块主体。一旦要是使用声明块主体,C# 2.0的匿名函数标记必须被使用。

【上篇】
【下篇】

抱歉!评论已关闭.