Overload resolution and null

My colleague Soundar discovered this rather interesting behavior. 1: class Test 2: { 3: public static void Main() 4: { 5: Test test = null; 6: 7: Console.WriteLine("{0}", test); 8: Console.WriteLine("{0}", null); 9: } 10: } If you run this code, you’ll find that while line 7 prints an empty line, line 8 causes an ArgumentNullException. Note that the test reference is also null, so it should certainly surprise you that the two lines result in different behavior at runtime. It certainly surprised me enough to make me dig deeper into the reason for the difference. I reasoned that given … Continue reading Overload resolution and null

Volatile and local

If you’ve done any multithreading programming at all, you must be aware of the volatile modifier. When a field is marked volatile, it tells 1. the JIT compiler that it can’t hoist the field because it may be modified by multiple threads 2. the CLR that the field must be read to and written from with acquire and release semantics. Given what you’ve read above, the post’s title doesn’t make sense. A local variable, by definition, cannot be accessed from multiple threads. An object referred to by a local variable can be shared among threads, but never the variable itself. … Continue reading Volatile and local