lambda 表达式和匿名方法之间的区别 - C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/761236/
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
Difference between lambda expressions and anonymous methods - C#
提问by Bob Smith
Duplicate:delegate keyword vs. lambda notation
I understand the anonymous methods can be used to define delegates and write inline functions. Is using Lambda expressions any different from this?
我了解匿名方法可用于定义委托和编写内联函数。使用 Lambda 表达式与此有什么不同吗?
I guess I am a little confused on when to use what.
我想我对何时使用什么有点困惑。
Edit: Also, appears that to use either anonymous or lambdas, there needs to be an Extension method for the type?
编辑:此外,似乎要使用匿名或 lambdas,该类型需要一个 Extension 方法?
回答by JaredPar
Not really no. They are essentially the exact same feature with different syntax constructs. The general shift appears to be away from the C# 2.0 anonymous method syntax towards the lambda style syntax for both anonymous expressions and functions though.
不是真的没有。它们本质上是完全相同的功能,但具有不同的语法结构。不过,对于匿名表达式和函数,总体转变似乎是从 C# 2.0 匿名方法语法转向 lambda 风格的语法。
回答by Adam Alexander
A lambda expression is simply shortcut syntax for an anonymous method. Anonymous methods look like this:
lambda 表达式只是匿名方法的快捷语法。匿名方法如下所示:
delegate(params) {method body}
The equivalent lambda expression would look like this:
等效的 lambda 表达式如下所示:
params => method body
In short, all lambda expressions are anonymous methods, but it is possible to have an anonymous method that is not written in lambda syntax (like the first example above). Hope this is helpful!
简而言之,所有 lambda 表达式都是匿名方法,但也可能有一个匿名方法不是用 lambda 语法编写的(如上面的第一个示例)。希望这是有帮助的!
回答by laktak
Here's a good explanation: C#: delegate keyword vs. lambda notation
这是一个很好的解释:C#:delegate 关键字与 lambda 符号
回答by bdukes
Lambda expressions can be converted to expression trees, while anonymous delegates cannot.
Lambda 表达式可以转换为表达式树,而匿名委托则不能。
回答by Valentin Kuzub
Only lambda expression without method body can be converted to expression tree
只有没有方法体的 lambda 表达式才能转换为表达式树
Following constructs docompile:
以下构造可以编译:
Func<int> exp1 = () => 1;
Func<int> exp2 = () => { return 1; };
Func<int> exp3 = delegate { return 1; };
Expression<Func<int>> exp4 = () => 1;
And following do not
而以下不
Expression<Func<int>> exp5 = delegate { return 1; }; //no anonymous delegates
Expression<Func<int>> exp6 = () => { return 1; }; //or lambdas with block body
So there is difference even on not very advanced level ( that Jon Skeet points out here sick difference example)
所以即使在不是很高级的水平上也存在差异(乔恩斯基特在这里指出病态差异示例)
Another difference is that you can create anonymous delegates without parameter list if you don't plan to use them inside method body, with lambda you always have to provide parameters.
另一个区别是,如果您不打算在方法主体中使用匿名委托,则可以创建没有参数列表的匿名委托,而对于 lambda,您始终必须提供参数。
Following two lines demonstrate the difference
以下两行说明了区别
Func<int, int, int, int, int> anonymous = delegate { return 1; };
Func<int, int, int, int, int> lambda = (param1, param2, param3, param4) => 1;
You do essentially the same thing but anonymous delegate clearly looks better here.
你基本上做同样的事情,但匿名委托在这里显然看起来更好。