在 C# 中编写匿名函数的推荐方法是什么?
var seq = Enumerable.Range(1, 10).Reverse();
var sort1 = seq.OrderBy(i => i);
var sort2 = seq.OrderBy(delegate(int i) { return i; });
我认为 sort2 更明确,但 sort 1 更短。 除此之外,我真的不知道有什么区别。 推荐的方法是什么?
var seq = Enumerable.Range(1, 10).Reverse();
var sort1 = seq.OrderBy(i => i);
var sort2 = seq.OrderBy(delegate(int i) { return i; });
i think sort2 is more explicit but sort 1 is shorter. besides that, i don't really know the difference. what is the recommended way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在任何情况下,Lambda 表达式(IMO)都比匿名方法更好,除非您不关心参数,在这种情况下,有一个很好的快捷方式:
Lambda 表达式还有另外两个“问题”IMO:
无参数 lambda 表达式的语法有些笨拙:
<前><代码>() =>; 东西
Lambda expressions are (IMO) better than anonymous methods in every case except where you don't care about the parameters, in which case there's a nice shortcut:
Lambda expressions have two other "problems" IMO:
The syntax for a parameterless lambda expresssion is somewhat clunky:
如果可能的话,我更喜欢 lambda 语法 (sort1)。 我只在需要时使用更详细的语法。 我认为额外的东西是非生产性代码,只会妨碍理解我所写的内容。
编辑:当然,除非我正在开发 .NET 2.0 应用程序,否则您无法使用 lambda 语法。 然后,我很高兴我至少有匿名方法。
I much prefer the lambda syntax (sort1) where possible. I only use the more verbose syntaxes where they are required. I consider the extra stuff non-productive code that just gets in the way of understanding what I'm writing.
Edit: Unless of course I'm working on a .NET 2.0 app, where you can't use the lambda syntax. Then, I'm just glad I at least have anonymous methods.
我发现这取决于具体情况,因为重要的方面是确保代码的意图为未来的维护人员提供了良好的记录。 因此,有时 lambda 效果最好,但有时匿名方法是更好的选择。 需要多个参数的 lambda 的问题是语法开始显得混乱,因此有时使用匿名方法委托语法来提供更容易识别的结构会很有用。
在您给出的示例中,lambda 是更好的选择,因为它清晰简洁; 但是,当声明内联事件处理程序时,匿名方法可能会提供更好的解决方案(在可维护性方面)。
I find this depends on the scenario as the important aspect is making sure the intent of your code is well documented for future maintainers. Therefore, sometimes a lambda works best, but other times an anonymous method is a better option. The problem with lambdas where more than argument is needed, is that the syntax starts to look cluttered, so it can sometimes be useful to use the anonymous method delegate syntax to provide a more recognisable structure.
In the example you have given, the lambda is the better option as it is clear and concise; however, when declaring say an inline event handler, an anonymous method might provide a better solution (with regards to maintainability).
与 lambda 表达式相比,我喜欢委托语法的地方很少(我只能想到其中之一)
……例如。 其余时间,委托只是碍事。 根据乔恩的评论...
There are few (and I can only think of one off the top) where I like the delegate syntax over the lambda expression ...
... for example. The rest of the time, delegate just gets in the way. Per Jon's comment ...
据我所知,这两种方法都会生成完全相同的 IL 代码(至少在本例中),所以这实际上是一个品味问题。 我经常认为 lambda 味道更好。
As far as I know both approaches will generate the exact same IL code (at least in this example), so it's really a matter of taste. I often think lambda tastes nicer.
sort1 - 我认为 Lambda 是编写匿名方法的首选方式
sort1 - Lambdas I think is the prefered way to write Anonymous methods
我更喜欢 lambda 表达式,因为它们提供最简洁的语法,并且类似于此构造最初来源的函数式编程语言中的 lambda。 当表达式包含多个语句或非功能性构造时,我将使用
delegate
。 无参数、多行或显式类型的 lambda 表达式可能看起来有些奇怪,因此失去了它们的主要优势(更具可读性)。I prefer lambda-expressions since they offer the the most concise syntax and resemble the lambdas in functional programming languages where this construct originally comes from. When the expression contains more than one statement or unfunctional constructs, I'll use
delegate
. Parameterless, multi-lined or explicitly typed lambda expressions may look somewhat weird and hence lose their major advantage (being more readable).