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?