最佳答案
编写内联事件处理程序是不好的做法吗?
对我来说,我更喜欢在事件处理程序中使用局部变量时使用它,如下所示:
我更喜欢这样:
// This is just a sample
private void Foo()
{
Timer timer = new Timer() { Interval = 1000 };
int counter = 0; // counter has just this mission
timer.Tick += (s, e) => myTextBox.Text = (counter++).ToString();
timer.Start();
}
而不是这样:
int counter = 0; // No need for this out of Boo & the event handler
private void Boo()
{
Timer timer = new Timer() { Interval = 1000 };
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
myTextBox.Text = (counter++).ToString();
}