匿名方法 (C# 2.0) 和 lambda 表达式 (C# 3.0) 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/208381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:06:45  来源:igfitidea点击:

What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)?

c#methodsexpression

提问by Codeslayer

What is the difference between anonymous methodsof C# 2.0 and lambda expressionsof C# 3.0.?

C# 2.0 的匿名方法和C# 3.0 的lambda 表达式有什么区别?

采纳答案by Brian R. Bondy

The MSDN page on anonymous methods explains it

匿名方法的 MSDN 页面解释了它

In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).

在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法。C# 2.0 引入了匿名方法,在 C# 3.0 及更高版本中,lambda 表达式取代匿名方法成为编写内联代码的首选方式。但是,本主题中有关匿名方法的信息也适用于 lambda 表达式。在一种情况下,匿名方法提供了 lambda 表达式中没有的功能。匿名方法使您可以省略参数列表,这意味着匿名方法可以转换为具有各种签名的委托。这对于 lambda 表达式是不可能的。有关 lambda 表达式的更多信息,请参阅 Lambda 表达式(C# 编程指南)。

And regarding lambda expressions:

关于 lambda 表达式

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

Lambda 表达式是一个匿名函数,可以包含表达式和语句,可用于创建委托或表达式树类型。所有 lambda 表达式都使用 lambda 运算符 =>,它被读作“goes to”。lambda 运算符的左侧指定输入参数(如果有),右侧保存表达式或语句块。lambda 表达式 x => x * x 读作“x 到 x 乘以 x”。此表达式可以分配给委托类型,如下所示:

回答by Marc Gravell

First, convenience: lambdas are easier to read and write.

首先,便利性:lambda 更易于阅读和编写。

Second, expressions: lambdas can be compiled to eithera delegate, oran expression tree (Expression<T>for some delegate-type T, such as Func<int,bool>). Expression trees are the more exciting, as it is the key to LINQ to out-of-process data stores.

第二,表达式:lambda表达式可被编译以任一代表,一个表达式树(Expression<T>对于一些代表类型T,如Func<int,bool>)。表达式树更令人兴奋,因为它是 LINQ 到进程外数据存储的关键。

Func<int,bool> isEven = i => i % 2 == 0;
Expression<Func<int,bool>> isEven = i => i % 2 == 0;

Note that lambda expressions with statement bodies can only be compiled to delegates, not Expressions:

请注意,带有语句主体的 lambda 表达式只能编译为委托,而不能编译为Expressions:

Action a = () => { Console.WriteLine(obj.ToString()); };

回答by Pop Catalin

  1. Anonymous methods are basically functions without a name, with the ability to create closures.
  2. Lambda expressions are constructs that are convertible to both anonymous methods and expression trees, and follow more complex rules of type inference than anonymous methods.
  1. 匿名方法基本上是没有名称的函数,具有创建闭包的能力。
  2. Lambda 表达式是可转换为匿名方法和表达式树的构造,并且遵循比匿名方法更复杂的类型推断规则。

The range of more or less subtle differences are explained by Eric Lippert (C# language designer) in his blog:

Eric Lippert(C# 语言设计师)在他的博客中解释了或多或少的细微差异范围:

回答by Jon Skeet

  1. Lambda expressions can be converted to delegates or expression trees (with some restrictions); anonymous methods can only be converted to delegates
  2. Lambda expressions allow type inference on parameters:
  3. Lambda expressions allow the body to be truncated to just an expression (to return a value) or single statement (in other cases) without braces.
  4. Lambda expressions allow the parameter list to be shortened to just the parameter name when the type can be inferred and when there's only a single parameter
  5. Anonymous methods allow the parameter list to be omitted entirely when it's not used within the body and it doesn't lead to ambiguity
  1. Lambda 表达式可以转换为委托或表达式树(有一些限制);匿名方法只能转换为委托
  2. Lambda 表达式允许对参数进行类型推断:
  3. Lambda 表达式允许将主体截断为不带花括号的仅表达式(以返回值)或单个语句(在其他情况下)。
  4. 当可以推断类型并且只有一个参数时,Lambda 表达式允许将参数列表缩短为仅参数名称
  5. 匿名方法允许在参数列表不在正文中使用时完全省略它,并且不会导致歧义

The last point is the only benefit of anonymous methods over lambdas, I believe. It's useful to create a field-like event with a no-op subscription though:

我相信最后一点是匿名方法相对于 lambda 的唯一好处。但是,创建具有无操作订阅的类似字段的事件很有用:

public event EventHandler Click = delegate{};