(this == null) in C#!

Posted by SLaks on Stack Overflow See other posts from Stack Overflow or by SLaks
Published on 2009-10-21T12:57:10Z Indexed on 2010/05/07 16:18 UTC
Read the original article Hit count: 220

Due to a bug that was fixed in C# 4, the following program prints true. (Try it in LINQPad)

void Main() { new Derived(); }

class Base {
    public Base(Func<string> valueMaker) { Console.WriteLine(valueMaker()); }
}
class Derived : Base {
    string CheckNull() { return "Am I null? " + (this == null); }
    public Derived() : base(() => CheckNull()) { }
}

In VS2008 in Release mode, in throws an InvalidProgramException. (In Debug mode, it works fine)

In VS2010 Beta 2, it doesn't compile (I didn't try Beta 1); I learned that the hard way

Is there any other way to make this == null in pure C#?

© Stack Overflow or respective owner

Related posts about compiler-bug

Related posts about c#