0%

C# Puzzle: What does this program do?

Consider the following code:

class Woot
{
private static float PI;
private static bool initialized = doInitialize();

private static bool doInitialize()
{
if (!initialized)
{
var thread = new Thread(() => { PI = 3.14f; });
thread.Start();
thread.Join();
}
return true;
}

public static void Main(string[] args)
{
Console.WriteLine(PI);
}
}

What is the output of this program? Is it:

  1. 3.14
  2. 0
  3. Throws exception
  4. None of the above

To find out the answer to this puzzle, as well as many others, watch the C# Puzzlers talk with Eric Lippert and Neal Gafter from NDC 2010. How many did you get right?