I'm trying to understand why the following program gives the output that it does. I know it has something to do with references and values but I neither know the terminology nor do I know where to go to learn more.
for (int x = 0; x < 2; x++)
{
int y = x;
new Thread(new ThreadStart(() =>
{
Thread.Sleep(100);
Console.WriteLine("Thread sees x = {0}, y = {1}", x, y);
})).Start();
}
Thread.Sleep(1000);
Output:
Thread sees x = 2, y = 0
Thread sees x = 2, y = 1
A reference explaining this sort of thing would be very much appreciated.