Possible Duplicate:
C#: using the iterator variable of foreach loop in a lambda expression – why fails?
I was reading c# reference at MSDN, and I found this..
http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx
at the end in comments there is one comment by albionmike
It goes like this..
When you "catpure" a variable from an outer scope, some counter-intuitive things happen.
If you run this, you will get an IndexOutOfRange exception during the call f().
If you uncomment the two commented out lines of code, it will work as expected.
Hint: Captured Outer Variables have reference rather than value semantics
// Console Project
using System;
using System.Collections.Generic;
using System.Text;
namespace EvilDelegation
{
delegate void PrintIt();
class Program
{
static void Main(string[] args)
{
string[] strings = { "zero", "one", "two", "three", "four" };
PrintIt f = null;
for (int i = 0; i < strings.Length; ++i) {
if (i == 2 || i == 3) {
// Can you see why this would not work?
f = delegate() { Console.WriteLine(strings[i]); };
// But this does...
//int k = i;
//f = delegate() { Console.WriteLine(strings[k]); };
}
}
f();
}
}
}
I don’t get it, why the fist one won’t work, and the second one will? In the 4th line, he says: Captured Outer Variables have reference rather than value semantics.
Okay, fine. But in the for loop, we defined i as an int which of course is a value type, so how can an int type hold a reference? And if i cannot hold reference, that means it is storing value, and if it is storing value, then I don’t get why the first one wont work and second one will?
Am I missing something here?
EDIT : I think the original author had a typo the call to f() should have been inside the if loop. Please consider that this while answering.
EDIT 2 : Okay, in case someone might say, it was not a typo, let consider that it was. I wanna know the case where the call to f() is made inside the if clause. Would both run in that case, or just the one not commented?
This is because of the semantics of closures. When a closure references a local variable in its outer scope, it captures a reference to the variable, not the value contained in the variable.
In this case, the anonymous delegate
delegate() { Console.WriteLine(strings[i]); }is capturing a reference to theivariable; that is, the variable is shared between the anonymous function and the scope in whichiwas declared. Whenichanges in one context, it also changes in the other.For example (see it run):
This will output:
In C#, the lifetime of a local is extended to include the lifetime of any closures that reference them. This enables some pretty interesting tricks that may offend the sensibilities of C/C++ developers: