1 min
––– views

A fancy 'is not null' checker for C#

The classic way of check if an reference is not null is the following:

csif (thing != null)

But, what happens if someone overload the != operator? We cannot guarantee that is an object. But, using the following pattern, that wouldn't matter:

csif (thing is object)

Also, we can use it to check if a reference is null:

csif (thing is null)

There are developers who can argue that in this way (first case) the intention of checking if the variable has a non-null instance is being obfuscated. But I think it's more idiomatic, at least once you get used to it. What do you think?