My understanding is that the "Access to modified closure" warning is there to warn me about accessing local variables from a delegate when the delegate might be stored and called later or called on a different thread so that the local variable isn't actually available at the time of actual code execution. This is sensible of course.
But what if I am creating a delegate that I know is going to be called immediately in the same thread? The warning then is not needed. For example the warning is generated in the code:
delegate void Consume();
private void ConsumeConsume(Consume c)
{
c();
}
public int Hello()
{
int a = 0;
ConsumeConsume(() => { a += 9; });
a = 1;
return a;
}
There can be no problem here since ConsumeConsume always calls the function immediately. Is there any way around this? Is there some way to annotate the function ConsumeConsume to indicate the ReSharper that the delegate will be called immediately?
Interestingly, when I replace the ConsumeConsume(() => { a += 9; }); line with:
new List<int>(new[] {1}).ForEach(i => { a += 9; });
which does the same thing, no warning is generated. Is this just an in-built exception for ReSharper or is there something I can do similarly to indicate that the delegate is called immediately?
I am aware that I can disable these warnings but that is not a desired outcome.