Is it bad practice to write inline event handlers?
For me, I prefer to use it when I want to use a local variable in an event handler, as shown below:
I prefer this:
// 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(); }
Instead of this:
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(); }
c # event-handling inline-code
Homam Oct. 31 '10 at 15:41 2010-10-31 15:41
source share