Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这绝对没问题 - 尽管有两个警告:
通常我只内联非常简单的事件处理程序 - 对于更多涉及的内容,我使用 lambda 表达式(或匿名方法)通过调用以下方法来订阅更合适的方法:
It's absolutely fine - although there are two caveats:
Typically I only inline really simple event handlers - for anything more involved, I use lambda expressions (or anonymous methods) to subscribe with a call to a method with a more appropriate method:
在大多数情况下,我宁愿使用像“timer_Tick()”这样的单独方法,但我更愿意将其称为 OnTimerTick() ,如下所示:
但是,如果事件仅在内联声明的方法返回之前触发,并且设置事件的对象的范围仅限于声明方法,那么我认为“内联”版本更好。因此,我喜欢使用“in line”将比较委托传递给“sort”方法。
In most cases I would rather have the separate methods like “timer_Tick()”, however I should rather it be called OnTimerTick() as:
However if the event will only be fired before the method it is declared in-line returns and the object the event is set on has a scope what that is limited to the declaring method, then I think the “in line” version is better. Hence I like using “in line” for the compare delegate being passed to a “sort” method.
您将两个样本放在一起。显然,第二个选项(您不喜欢它)是最具可读性的。
代码的可读性和可维护性非常重要。保持事情简单,尽可能容易理解。大多数人通常认为 Lambda 表达式更难理解。即使它们对你来说是第二天性,对其他人来说可能不是。
You put the two samples together. It is clear that the second option (which you don't prefer) is the most readable.
Code readability and maintainability are very important. Keep things simple, as easy as possible to understand. Lambda expressions are generally considered harder to understand by majority of people. Even if they are a second nature for you for others might not.