Suppose we have the following code:
void AFunction()
{
   foreach(AClass i in AClassCollection)
   {
      listOfLambdaFunctions.AddLast(  () =>  {  PrintLine(i.name); }  );
   }
}
void Main()
{
    AFunction();
    foreach( var i in listOfLambdaFunctions)
       i();
}
One might think that the above code would out the same as the following:
void Main()
{
    foreach(AClass i in AClassCollection)
       PrintLine(i.name);
}
However, it doesn't. Instead, it prints the name of the last item in AClassCollection every time.
It appears as if the same item was being used in each lambda function. I suspect there might be some delay from when the lambda was created to when the lambda took a snapshot of the external variables used in it.
Essentially, the lambda is holding a reference to the local variable i, instead of taking a "snapshot" of i's value when the lambda was created.
To test this theory, I tried this code:
string astr = "a string";
AFunc fnc = () => { System.Diagnostics.Debug.WriteLine(astr); };
astr = "changed";
fnc();
and, surprise, it outputs changed!
I am using XNA 3.1, and whichever version of C# that comes with it.
My questions are:
- What is going on?
- Does the lambda function somehow store a 'reference' to the variable or something?
- Is there any way around this problem?
 
     
     
     
     
     
     
    