Random header image... Refresh for more!

This Code Doesn’t Work

The following is a snippet of code that reproduces a bug I came across today.

string[] strings = new string[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

foreach (string str in strings)
{
    ThreadPool.QueueUserWorkItem( delegate { Console.WriteLine(str); } );
}

It should print out the numbers “zero” through “nine”.  It doesn’t work.  Why not?

I’ll write up the answer tomorrow, but I want to see if anyone else can get it first.

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment