Still confused about "Access to modified closure"

I've tried to understand what "access to modified closure" is all about several times over the last three years or so, and the only thing I'm sure of is that I'm still confused about it.

In searching once again for enlightenment, I came across this thread:

The final thread entry says, "Yes, 3 new attributes are added."

Could someone please expand on this? What are the three new attributes that are being talked about? And how should they be used to avoid the "access to modified closure" warning in situations where it should be possible to avoid that warning?

Apropos JetBrains.Annotations attributes, the current web help page lists 28 attributes. But Intellisense on JetBrains.Annotations lists 52 possible attributes! I'm wondering if the three attributes mentioned above are among the ones that aren't documented on the help page.


1 comment
Comment actions Permalink

I've now gone through all of the places in my code (approx. 40) where I had suppressed the "access to modified closure" warnings from ReSharper 5. ReSharper 7.1 seems to be much better at not giving false alarms, which is good - too many false alarms and you don't notice the real alarms.

Some places were still giving warnings. These involved a class of my own that wrapped List<>, so use of MyList.Find() in a "foreach" resulted in a warning. I figured out that I should put the [InstantHandle] attribute on the parameter of the Find() method, and the Exists() method, etc. Then all of the warnings were gone, so I feel justified in having suppressed these warnings during the last three years.

Recommendations to JetBrains:

1. There should be better documentation about how to deal with the "access to modified closure" warnings. Which scenarios can be ignored, how to systematically suppress warnings with  [InstantHandle] when applicable, and which scenarios are not to be ignored.

2. Specifically, this page needs more information, and also needs updating to reflect the changes in C# 5.0.

It was just by chance that I stumbled on how to use the [InstantHandle] attribute - it shouldn't be so difficult to find this information.



Please sign in to leave a comment.