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 {
pu
The raw decompilation (Reflector with no optimizations) of the Debug mode binary is:
private class Derived : Program.Base
{
// Methods
public Derived()
{
base..ctor(new Func(Program.Derived.<.ctor>b__0));
return;
}
[CompilerGenerated]
private static string <.ctor>b__0()
{
string CS$1$0000;
CS$1$0000 = CS$1$0000.CheckNull();
Label_0009:
return CS$1$0000;
}
private string CheckNull()
{
string CS$1$0000;
CS$1$0000 = "Am I null? " + ((bool) (this == null));
Label_0017:
return CS$1$0000;
}
}
The CompilerGenerated method doesn't make sense; if you look at the IL (below), it's calling the method on a null string (!).
.locals init (
[0] string CS$1$0000)
L_0000: ldloc.0
L_0001: call instance string CompilerBug.Program/Derived::CheckNull()
L_0006: stloc.0
L_0007: br.s L_0009
L_0009: ldloc.0
L_000a: ret
In Release mode, the local variable is optimized away, so it tries to push a non-existant variable on to the stack.
L_0000: ldloc.0
L_0001: call instance string CompilerBug.Program/Derived::CheckNull()
L_0006: ret
(Reflector crashes when turning it into C#)
EDIT: Does anyone (Eric Lippert?) know why the compiler emits the ldloc?