Possible Duplicate:
C# Captured Variable In Loop
I'm working on a few simple applications of threading, but I can't seem to get this to work:
class ThreadTest
{
static Queue<Thread> threadQueue = new Queue<Thread>();
static void Main()
{
//Create and enqueue threads
for (int x = 0; x < 2; x++)
{
threadQueue.Enqueue(new Thread(() => WriteNumber(x)));
}
while(threadQueue.Count != 0)
{
Thread temp = threadQueue.Dequeue();
temp.Start();
}
Console.Read();
}
static void WriteNumber(int number)
{
for (int i = 0; i < 1000; i++)
{
Console.Write(number);
}
}
}
The goal basically is to add threads to a queue one by one, and then to go through the queue one by one and pop off a thread and execute it. Because I have "x<2" in my for loop, it should only make two threads - one where it runs WriteNumber(0), and one where it runs WriteNumber(1), which means that I should end up with 1000 0's and 1000 1's on my screen in varying order depending on how the threads are ultimately executed.
What I end up with is 2000 2's. The two possible solutions that I've come up with are: I've missed something glaringly obvious, or sending the variable x to the WriteNumber function is doing a pass-by-reference and not pass-by-value, so that when the threads execute they're using the most recent version of x instead of what it was at the time that the function was set. However, it was my understanding that variables are passed by value by default in C#, and only passed by reference if you include 'ref' in your parameter.