在 C# 中编写匿名函数的推荐方法是什么?

发布于 2024-07-25 04:46:19 字数 226 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

逆光下的微笑 2024-08-01 04:46:19

在任何情况下,Lambda 表达式(IMO)都比匿名方法更好,除非您不关心参数,在这种情况下,有一个很好的快捷方式:

// Lambda expression has to specify parameter types
EventHandler x = (sender, args) => Console.WriteLine("Hi");

// Anonymous method can ignore them
EventHandler x = delegate { Console.WriteLine("Hi"); };

Lambda 表达式还有另外两个“问题”IMO:

  • 显然如果您不使用 C# 3,它们将不可用。(尽管您可以从 VS2008 以 .NET 2.0 为目标并仍然使用它们。)
  • 无参数 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 expression has to specify parameter types
EventHandler x = (sender, args) => Console.WriteLine("Hi");

// Anonymous method can ignore them
EventHandler x = delegate { Console.WriteLine("Hi"); };

Lambda expressions have two other "problems" IMO:

  • Obviously they're not available if you're not using C# 3. (Although you can target .NET 2.0 from VS2008 and still use them.)
  • The syntax for a parameterless lambda expresssion is somewhat clunky:

    () => stuff
    
执手闯天涯 2024-08-01 04:46:19

如果可能的话,我更喜欢 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.

爱你不解释 2024-08-01 04:46:19

我发现这取决于具体情况,因为重要的方面是确保代码的意图为未来的维护人员提供了良好的记录。 因此,有时 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).

夜血缘 2024-08-01 04:46:19

与 lambda 表达式相比,我喜欢委托语法的地方很少(我只能想到其中之一)

public event Action Evt = delegate {};
public event Action Evt = () => { };

……例如。 其余时间,委托只是碍事。 根据乔恩的评论...

public event EventHandler Evt = delegate {};
public event EventHandler Evt = (s,ea) => { };

There are few (and I can only think of one off the top) where I like the delegate syntax over the lambda expression ...

public event Action Evt = delegate {};
public event Action Evt = () => { };

... for example. The rest of the time, delegate just gets in the way. Per Jon's comment ...

public event EventHandler Evt = delegate {};
public event EventHandler Evt = (s,ea) => { };
栀梦 2024-08-01 04:46:19

据我所知,这两种方法都会生成完全相同的 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.

他夏了夏天 2024-08-01 04:46:19

sort1 - 我认为 Lambda 是编写匿名方法的首选方式

sort1 - Lambdas I think is the prefered way to write Anonymous methods

小霸王臭丫头 2024-08-01 04:46:19

我更喜欢 lambda 表达式,因为它们提供最简洁的语法,并且类似于此构造最初来源的函数式编程语言中的 lambda。 当表达式包含多个语句或非功能性构造时,我将使用delegate。 无参数、多行或显式类型的 lambda 表达式可能看起来有些奇怪,因此失去了它们的主要优势(更具可读性)。

g = (x => 2 * f(x) + 1); // Nice functional one-liner

// but

action = delegate (int arg) {
    SendSystemMessage(arg);
    Console.WriteLine("Got value: {0}", arg);
};

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).

g = (x => 2 * f(x) + 1); // Nice functional one-liner

// but

action = delegate (int arg) {
    SendSystemMessage(arg);
    Console.WriteLine("Got value: {0}", arg);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文