What does "Access to modified closure" mean?

Hello

I have the following code:

01 foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
02 {
03 if(list.Count(t => t.Name == info.Id) == 0)
04 {
05 result = false;
06 }
07 }

In line 03 I see that info is underlined and the message is "Access to
modified closure" and I get the refactoring option to copy it to a local
variable resulting in this:

01 foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
02 {
03 TimeZoneInfo info1 = info;
04 if(list.Count(t => t.Name == info1.Id) == 0)
05 {
06 result = false;
07 }
08 }

What exactly is meant with "Access to modified closure"?

Gabriel Lozano-Moran

5 comments

It has to do with scoping rules of closures. If you were store the delegate
and execute it later, it would not behave as you might expect.

For a more detailed discussion, see the following blog entries:
http://blogs.msdn.com/abhinaba/archive/2005/10/18/482180.aspx

http://blogs.msdn.com/oldnewthing/archive/2006/08/02/686456.aspx
http://blogs.msdn.com/oldnewthing/archive/2006/08/03/687529.aspx
http://blogs.msdn.com/oldnewthing/archive/2006/08/04/688527.aspx



"Gabriel Lozano-Moran" <gabriel.lozano-moran@hotmail.com> wrote in message
news:fpe6n6$t0h$1@is.intellij.net...

Hello

>

I have the following code:

>

01 foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
02 {
03 if(list.Count(t => t.Name == info.Id) == 0)
04 {
05 result = false;
06 }
07 }

>

In line 03 I see that info is underlined and the message is "Access to
modified closure" and I get the refactoring option to copy it to a local
variable resulting in this:

>

01 foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
02 {
03 TimeZoneInfo info1 = info;
04 if(list.Count(t => t.Name == info1.Id) == 0)
05 {
06 result = false;
07 }
08 }

>

What exactly is meant with "Access to modified closure"?

>

Gabriel Lozano-Moran


0

Is it still a valid warning if using value types (i.e. struct)?

0

Hello Francois,

Yes, this is still a valid warning for value types. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Is it still a valid warning if using value types (i.e. struct)?

---
Original message URL:
http://devnet.jetbrains.net/message/5274174#5274174



0

Please sign in to leave a comment.