0

Recently, while doing input checks I (probably by mistake) typed something like this:

if (someObject is null) { }

instead of classic:

if (someObject == null) { }

I found it more elegant as it is more descriptive by itself. It turns out it behaves correctly (at least on simple cases) and when something is null the expression returns true (and false when it is not).

Does it have any disadvantages over 'classic' way?

michal-mad
  • 432
  • 4
  • 11
  • 2
    Surely you mean == null ? – Emond May 14 '19 at 14:46
  • Ah, yes. Thanks! – michal-mad May 14 '19 at 14:47
  • The linked previous answer probably explains everything. Regarding your statement that you found ``is null`` more elegant than ``== null``: surely every C# developer knows what the ``==`` operator is for, and given that you don't even save any characters, calling the use of ``is`` more elegant is, in my opinion, a bit overstated. More elegant would be using other constructions that abstract away the question of nullness, such as an option type together with proper methods to handle lifting values into and out of it. – dumetrulo May 14 '19 at 15:23

0 Answers0