16

C# how to check for null. (value is null) or (null == value). Can we use is operator instead of == operator?

C# 7.0 supports const pattern with is operator. So we can use is null for all null checking ?

Can the object be empty as well besides being null?

knocte
  • 16,941
  • 11
  • 79
  • 125
Shahid Roofi Khan
  • 957
  • 1
  • 8
  • 19

1 Answers1

37

Yes, you can use the is operator with the constant pattern of null to check whether a reference (or nullable value type value) is null or not.

Indeed, in C# 7 I would say that using is null is the most idiomatic way of testing for a null value, because it doesn't use any user-defined operators. Consider this:

string x = GetStringFromSomewhere();

if (x == null) { }  // Option 1
if (x is null) { }  // Option 2

Here, option 1 will call the == operator overload defined in string. While that should do what you want (and I expect the JIT compiler will optimize it pretty heavily), it's not like you particularly want to do that - you just want to test whether the value of x is a null reference. That's exactly what option 2 does.

So yes, you can use is null for all null checking if you don't have types that perform odd custom comparisons. It's possible to write a class such that x == null and x is null would give different results, but that would almost always be a design (or implementation) problem.

There's no concept of an object being "empty" - and indeed it's not the object that's null. Leaving nullable value types aside for now, it's a reference that's null, not an object - a null value indicates the absence of an object. It's worth distinguishing carefully between objects and references in your mind.

Some specific object types have a concept of "empty" - for example, a string can be empty, or a collection - but those types have specific ways of testing for emptiness. There's no general concept of an object being empty.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have a feeling that something is lost due to a language barrier... I think the "empty" concept is referring to something like python, where an empty list returns false {if ([]): //Returns false} – Mars Nov 07 '18 at 07:37
  • @Mars: But there's no such concept in C# for a general object. There's the concept of an empty array, or an empty list etc - but you'd need to check for that specifically. – Jon Skeet Nov 07 '18 at 07:37
  • I know. Just think thats possibly where this question was coming from, even if it wasn't asked very clearly – Mars Nov 07 '18 at 07:39
  • 1
    @Mars: Have added a paragraph at the end of the answer to help with that. – Jon Skeet Nov 07 '18 at 08:05
  • Much better than the answer in the linked question. – Frank Hileman Oct 30 '19 at 17:39
  • @JonSkeet can you please expand a little bit over the phrase **So yes, you can use is null for all null checking if you don't have types that perform odd custom comparisons**. Based on my knowledge a programmer can write an overload of the equality operator (`==`) so that he or she messes up the behavior of the `== null` comparison. How is it possible to write a type such that the behavior of the `is null` comparison breaks ? Thanks in advance ! – Enrico Massone Jan 28 '20 at 09:12
  • 1
    @EnricoMassone: The point is if you *want* `== null` to sometimes return true for non-null values (a sort of "logical null") then if you change that comparison to `is null`, it won't behave the same way. But if `== null` returns true if and only if the left operand is actually a null reference, just use `is null` instead. – Jon Skeet Jan 28 '20 at 09:20