Является ли плохой практикой писать встроенные обработчики событий?
Для меня я предпочитаю использовать его, когда я хочу использовать локальную переменную в обработчике событий, как показано ниже:
Я предпочитаю это:
// 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();
}