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, it 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#?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…