From this answer https://stackoverflow.com/a/6457528/299110
I’m using ctrl.PreRender += (sender, e) => ControlPreRender(ctrl, rule); to one or more controls within a foreach, with the values of ctrl and rule changing each time.
However when the ControlPreRender method is called, the rule parameter seems out of line with the sender the event handler was attached to.
I know I’m missing something here, not sure what though!
Update:
Thanks for the answers, Eric Lippert’s blogs really explained it. As suggested by the down-voter, I’ve put more of the code below, hopefully improving the question a bit:
foreach (var ctrl in controls)
{
// ...
foreach (var rule in rules)
{
// ...
ctrl.PreRender += (sender, e) => ControlPreRender(ctrl, rule);
}
}
public static void ControlPreRender(Control ctrl, ControlRule rule)
{
// ...
}
I think you want a temporary variable:
The reason is the following: Without that temporary variable, all your anonymous methods reference the same instance which changes as you loop through all your rules. This is called “access to modified closure”. As erikkallen mentions, this has been fixed in C# 5.
You can easily check that yourself: Set a breakpoint in
ControlPreRenderand on the first breakpoint hit make an object ID for the rule parameter. You will see that at all the following hits of your breakpoint the rule parameter will have the same object ID which means it is the exact same instance.