KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the last value in v: var funcs = new List<Func<int>>(); foreach (var v in values) { funcs.Add(() => v); } And the correct version looks like: foreach (var v in values) { int v2 = v; funcs.Add(() => v2); } Now my question is how and where are those captured 'v2' variables stored. In my understanding of the stack, all those v2 variables would occupy the same piece of memory. My first thought was boxing, each func member keeping a reference to a boxed v2. But that would not explain the first case.
Tags (comma-separated)
Save Edits
Cancel