That's interesting, and I concede that for string the Microsoft C# compiler appears to optimise away the superfluous call to the string equality, which is pretty clever.
That said, selecting pretty much any other class that overloads operator ==, my original contention holds
for example:
class Program
{
static Version foo;
static void Main(string[] args)
{
Console.WriteLine((object)foo == null);
}
}
compiles to
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 16 (0x10)
.maxstack 8
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Version ConsoleApplication1.Program::foo
IL_0006: ldnull
IL_0007: ceq
IL_0009: call void [mscorlib]System.Console::WriteLine(bool)
IL_000e: nop
IL_000f: ret
} // end of method Program::Main
whereas
class Program
{
static Version foo;
static void Main(string[] args)
{
Console.WriteLine(foo == null);
}
}
compiles to
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Version ConsoleApplication1.Program::foo
IL_0006: ldnull
IL_0007: call bool [mscorlib]System.Version::op_Equality(class [mscorlib]System.Version,
class [mscorlib]System.Version)
IL_000c: call void [mscorlib]System.Console::WriteLine(bool)
IL_0011: nop
IL_0012: ret
} // end of method Program::Main
But with strings, comparison to null does not require down-casting to object to get any speedup., so yes, you're right in this instance.