The Right Way to Check for Null in C#

preview_player
Показать описание

Hello, everybody, I'm Nick, and in this video, we will take a look at the right way to check for null in C#. C# has been around for a long time and the process of checking for null has evolved as the language itself has evolved.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

And for those wondering, both null-coalescing operators (??= and ??) and the ?. operator will be compiled to the "is null" version if the compiler detects that the operator is overloaded

nickchapsas
Автор

Let's be real, if the == operator is overriden in such a way that it can no longer check for null then the correct response is not to adjust your coding style, but to crucify the person wrote the override.

TKSSLCHN
Автор

I'm not going to lose any sleep over my code having `== null`. In over twenty years of programming, I've never seen the `==` operator overridden.

stevenbey
Автор

I've been using the "is null" for a while now purely because I think the syntax looks better and more readable (when you read the code in your head you're essentially translating "user == null" to "user is null" anyway), the fact you can't ever face issues with the equality operators being overridden is a nice bonus though

TkrZ
Автор

Unity dev here. I like that you mentioned the operator overloading in Unity, but it’s applicable only on object derived from UnityEngine.Object. On everything else and all your standard managed objects, you’re good to go with the « is » and « is not » operators. Good video though!

soverain
Автор

I pretty much like 'is' and 'is not', it is pretty readable.

diadetediotedio
Автор

I've never bought into the idea that we need to guard against someone overloading the == operator incorrectly.
Don't get me wrong.. the "is null" operator is fine but the idea that you would ever need to assume an operator overload is implemented to do the opposite of its intention is just insane.
We don't go around calling the `Add` method expecting it to subtract. Nor should we expect == to not equal.

ExpensivePizza
Автор

What I like most about your videos Nick, is that you explain the "why" behind it and I often learn just as much by the behind the scenes things as I do the intended topic. For instance, a while ago I picked up on your video about overloading default methods like !=, == etc. Thanks for all your videos!

countycowpoke
Автор

That's also how SQL server queries check for null values. So it is actually convenient to use the same syntax null check in server back-end and in database.

mohamedh.guelleh
Автор

If you find someone overwriting == the correct course of action is not to go along with what they are doing but instead to beat some sense into them. Overwriting == shouldn't be done lightly and if you must do it it should not impact existing code.

tropictiger
Автор

Great feature and a great video.
In fact I've actually written about "x is object" back in 2019 in a blog post I've named "The perfect non-null test". In c# 9 when they've added the not keyword it got that much better with "x is not null" - but still learned something new about how it actually works in lower c#, so thanks!

peledzohar
Автор

Things I learn from this video:
1. Operator overloading is really good in theory, but very bad in practice
2. C# is very complicated language, a simple thing like null check can be done in 10 different ways.

JosifovGjorgi
Автор

I like the "is null" because it's the same way you do it in SQL. And everyone knows SQL is the best programming language of all time

roelbrook
Автор

if == operator is overwritten and does not handle (x == null) case then it's either a bug or intended behavior

weedick
Автор

i like the new features, thanks for bringing them to our attention in a easily digestible way

notoriouslycuriouswombat
Автор

Personally, I enjoy using the maybe/option monad. It keeps my code clean from null checks, ensures that the caller handles both scenarios of when there is and isn't a result and it can handle any type, not relying on nullable being implemented.

antonbergstrom
Автор

I love this and I didn't knew about the weird "is not null". I wish C# would get the balls to say "okay, this will have breaking changes, we're removing all the useless stuff now" at least once a decade.

TheKoneko
Автор

I use the R#/Rider setting under code inspections to Colour Identifiers, which adds a bunch of semantic options for how to colour the code, which includes being able to chose different colours for normal operators vs overloaded operators.

GufNZ
Автор

As always an fantastic video! Could you do a video where you explain the different steps how C# code will be compile: Lowered, IL ... etc.? That would be awesome!

Schnickalodeon
Автор

Never thought about it this deeply actually. Good perspective check for me.

nove