Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "is object" instead of "!= null" or Object.ReferenceEquals() [closed]

Tags:

c#

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?

like image 991
Nexus Avatar asked Oct 10 '17 11:10

Nexus


People also ask

Is object in C#?

In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality. Object is a runtime entity, it is created at runtime.

Is Null Object C#?

null (C# Reference)The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.

IS NOT NULL check in C#?

C# | IsNullOrEmpty() Method In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


1 Answers

With

if (thing is object)
  ...

you obfuscate what you want to do - check for null. It might be obvious and clean to you at the moment, but it might not be in a few month (given you've dropped that practice) and it's certainly not obvious for anyone else. If I encountered this, it'd leave me puzzled about the intention of the author. And if you need a comment to explain what you do ... don't do it. (Of course there are situations where you can and should, but definitely not for something as simple as a null-check.)

Eventually you will render your code less maintainable, since understanding your code will always need a double take. Do yourself a favour and keep your code as clean as possible and this means being honest about your intentions.

like image 105
Paul Kertscher Avatar answered Oct 12 '22 23:10

Paul Kertscher