Monthly Archives: October 2010

Can (this == null): the answer, PLUS another Pop Quiz!

Pavel’s comment on my previous post was spot-on: this == null may evaluate to true if the == operator has been overloaded for the type in question.

I will give an example; but before I do, here’s another little teaser: can you come up with a program in which the following line compiles?

this = null;

Eh? Eh? What do you think?

New Pop Quiz! Can (this == null) ever evaluate to true?

Say I have this bit of C# code:

if (this == null)
    Console.WriteLine("Not null.");

Can the above ever produce the output “Null”?

(I happen to know the answer, which I will share in my next post. But I’ll let you puzzle over that one for now.)