Instantiation of local variables
A local variable is considered to be instantiated when execution enters the scope of the variable. For example, when the following method is invoked, the local variable x is instantiated and initialized three times—once for each iteration of the loop. static void F() { However, moving the declaration of x outside the loop results in a single instantiation of x: static void F() { When not captured, there is no way to observe exactly how often a local variable is instantiated—because the lifetimes of the instantiations are disjoint, it is possible for each instantation 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 void Main() { produces the output: 1 However, when the declaration of x is moved outside the loop: static D[] F() { the output is: 5 If a for-loop 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() { only one instance of the iteration variable is captured, which produces the output: 3 It is possible for anonymous function delegates to share some captured variables yet have separate instances of others. For example, if F is changed to static D[] F() { the three delegates capture the same instance of x but separate instances of y, and the output is: 1 1 Separate anonymous functions can capture the same instance of an outer variable. In the example: using System; delegate void Setter(int value); delegate int Getter(); class Test the two anonymous functions capture the same instance of the local variable x, and they can thus “communicate” through that variable. The output of the example is: 5
|