惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
U
Unit 42
MyScale Blog
MyScale Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
量子位
IT之家
IT之家
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
V
Visual Studio Blog
G
Google Developers Blog
Last Week in AI
Last Week in AI
雷峰网
雷峰网
博客园 - 聂微东
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
博客园 - 司徒正美
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏

博客园 - cp

HR:.Net(Senior)Software Engineer/Project Leader IT Consulting Positions (Senior Level) Software Sales Engineer / Software Engineer Scott Gu上海见面会 有个朋友公司急召C++程序员 对比面向对象和面向服务 预告一下接下来要写的心得 C# 3.0 规范(PDC2005)中文word版本 C#3.0规范(七)对象以及集合构造者 关于ComponentArt Web.UI 2006.1(build 1208)源代码之若干说明 ComponentArt Web.UI 2006.1 源代码 C#3.0规范(六)重载决断 C#3.0规范(五)类型推断 C#3.0规范(三)扩展函数 C#3.0(二)隐式类型化的本地变量 C#3.0规范(一)C#3.0概述 IIS不工作 Const n = 234234245# [导入]一个使用的小问题
C#3.0规范(四)Lambda 表达式
cp · 2006-05-14 · via 博客园 - cp
 

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的匿名函数标记必须被使用。