Lambda 闭包还是类级变量?
只是一个关于最佳实践的一般性问题:
public void Foo()
{
int x = 5;
myControl.Click += (o, e) =>
{
x = 6;
};
}
注意,我在 lambda 事件处理程序中使用 x 变量。
或者:
public class Bar
{
private int x = 5;
public void Foo()
{
Control myControl = new Control();
myControl.Click += new EventHandler(myControl_Click);
}
private void myControl_Click(object sender, EventArgs e)
{
x = 6;
}
}
这里,x
是该类的私有成员,因此我可以在事件处理程序中访问它。
现在假设我在代码中的其他任何地方都不需要 x
(无论出于何种原因),哪种方法是更好的方法?
Just a general question about what the best practice is:
public void Foo()
{
int x = 5;
myControl.Click += (o, e) =>
{
x = 6;
};
}
Notice, I'm using the x
variable inside my lambda event handler.
OR:
public class Bar
{
private int x = 5;
public void Foo()
{
Control myControl = new Control();
myControl.Click += new EventHandler(myControl_Click);
}
private void myControl_Click(object sender, EventArgs e)
{
x = 6;
}
}
Here, x
is a private member of the class, and therefore I have access to it in my event handler.
Now let's say I don't need x
anywhere else in the code (for whatever reason), which method is the better way to go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您的需要。 在第一个示例中,事件处理程序的副作用仅限于方法范围,而在第二个示例中,副作用是实例范围的。 我认为在第一个示例中使用闭包没有任何意义,因为 X 没有在任何地方使用,因此很难根据您的示例来确定。
话虽这么说,通常最好像对待变量一样对待事件处理程序(您在代码中创建的)。 尽可能缩小它们的范围,并根据需要将它们重构到更广泛的范围。
一个更好的示例强调了您何时应该使用闭包,如下所示:
这允许多个订阅者,并且比替代方案简单得多:
It depends on your need. In the first example, the side-effects of your event handler are limited to method scope, and in the second, the side-effects are instance scoped. I think that using a closure in terms of your first example serves no purpose since X isn't used anywhere, so it's hard to determine based on your examples.
That being said, it is generally best to treat event handlers (that you create in code) as you would variables. Scope them as narrowly as you can, and refactor them to broader scope as needed.
A better example that highlights when you should use a closure is as follows:
This allows multiple subscribers, and is much simpler than the alternative:
如果您在代码中的其他任何地方都不需要 x,则您的处理程序是无操作的 - 所以这肯定是无意义的情况。
一旦您确实需要x,您需要决定它的作用域是否应该是Bar实例或委托实例(或者可能是一些委托集合),这将决定您要做什么。
If you don't need x anywhere else in the code, your handler is a no-op - so surely that's a nonsense situation.
Once you do need x, you need to decide whether it should be scoped to the Bar instance or the delegate instance (or possibly some collection of delegates), and that will dictate what you do.