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.