Note on capturing value of a variable in an anonymous function

Extraction from “C# programming language 4th edition”

When not captured, there is no way to observe exactly how often a local variable is instan-tiated: Because the lifetimes of the instantiations are disjoint, it is possible for each
instantiation to simply use the same storage location. However, when an anonymous
function captures a local variable, the effects of instantiation become apparent.
The example

using System;
delegate void D();
class Test 
{
static D[] F() {
D[] result = new D[3];
for (int i = 0; i  { Console.WriteLine(x); };
}
return result;
}
static void Main() {
foreach (D d in F()) d();
} 
}

produces the following output:
1
3
5
However, when the declaration of xis moved outside the loop

static D[] F() {
D[] result = new D[3];
int x;
for (int i = 0; i  { Console.WriteLine(x); };
}
return result; 
}

the output is
5
5
5
If a forloop declares an iteration variable, that variable itself is considered to be declared
outside of the loop. Thus, if the example is changed to capture the iteration variable itself,

static D[] F() {
D[] result = new D[3];
for (int i = 0; i  { Console.WriteLine(i); };
}
return result; 
}
only one instance of the iteration variable is captured, which produces the following 
output:
3 
3 
3

The reason is C# capture the variable, not the value. It’s a bit hard to warp your head around this

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.