I’ve got code like the following:
private void SetupCheeseShop(Button buyCheese, Button spoilCheese)
{
var cheeseCount = 0; // No cheese
spoilCheese.Click += (sender, e) => {
// "Access to Modified Closure" warning occurs for cheeseCount below:
MessageBox.Show(string.Format("{0} cheeses have spoiled", cheeseCount));
cheeseCount = 0; // Throw out moldy cheese
};
buyCheese.Click += (sender, e) => {
cheeseCount++;
};
}
ReSharper is warning me that I’m accessing a modified closure when reading from cheeseCount in the spoil-cheese handler. Can I safely ignore it in this case?
I’m expecting the cheese count to be modified between calls to the first closure, but I’m not sure what happens when the code doing the modifying is in a second closure around the same variable.
You can safely ignore the warning in your case. It warns you that the closed over variable can potentially have a different value than the current value (0) at the time the closure is created. That’s what you want here. You can have any number of closures, they all reference the same variable. Think of a closure variable as a field in a class (which is what the compiler generates).
As stated in section §5.1.7 of the C# Language Specification 4.0:
You can safely rely on this behavior: the variable stays the same, no matter how many anonymous functions are referencing it, until the event handlers are collected, which won’t happen until the buttons themselves are collected in your example.