It has always bothered me that C# doesn't have a dedicated reference equality operator, one that can't be overloaded. To test if a reference is null, I want to write code like this:
if (thing == null)
But there's always this nagging thought, "What if the class has overloaded the == operator?". I'm not interested in whether the class considers the object equivalent to null. I'm interested in whether the object reference is null. The alternatives seem to be casting to object:
if ((object)thing == null)
and Object.ReferenceEquals():
if (Object.ReferenceEquals(thing, null)) // long form
if (ReferenceEquals(thing, null)) // short form
But recently I have been writing code like this:
if (thing is object) // thing != null
if (!(thing is object)) // thing == null
I read this as "if thing is an object", which is to say that it's set to an object. I realize this isn't the purpose of the "is" operator, but it does check for null references and all reference types inherit from object, so... why not?
I found that, to me at least, code like this is more readable and much more comfortable to type, especially since the affirmative case (thing is object) is much more common in my code than the negative case (!(thing is object)).
So my question is, are there any pitfalls or edge cases that I'm not aware of? Is it considered bad practice or inefficient? Is it confusing? Why don't I ever see code like this?