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:
- 3.14
- 0
- Throws exception
- 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?