P
puzzlecracker
I am not understanding what's happening in the code below (from
accelerated c#) -- I know what the outcome but not how .net does it:
using System;
using System.Linq;
public class LambdaTest
{
static void Main()
{
int counter = 0;
WriteStream( () => counter++ );
Console.WriteLine( "Final value of counter: {0}", counter );
}
static void WriteStream( Func<int> counter )
{
for( int i = 0; i < 10; ++i )
{
Console.Write( "{0}, ", counter() );
}
Console.WriteLine();
}
}
From what I understand, we are creating Func generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then why would counter
changed upon the retrun from WriteStream? Doesn't WriteSteam operate
on the local counter argument?
need some lambda education....
Thanks
accelerated c#) -- I know what the outcome but not how .net does it:
using System;
using System.Linq;
public class LambdaTest
{
static void Main()
{
int counter = 0;
WriteStream( () => counter++ );
Console.WriteLine( "Final value of counter: {0}", counter );
}
static void WriteStream( Func<int> counter )
{
for( int i = 0; i < 10; ++i )
{
Console.Write( "{0}, ", counter() );
}
Console.WriteLine();
}
}
From what I understand, we are creating Func generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then why would counter
changed upon the retrun from WriteStream? Doesn't WriteSteam operate
on the local counter argument?
need some lambda education....
Thanks