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

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

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