Are you checking for null correctly in C#? #Shorts

preview_player
Показать описание
Let's take a look at the evoluation of null checking in C# over the years.

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

I'm so proud that as a beginner I'm doing it correctly 😄Thanks to the wonderful C# community

josef
Автор

Galaxybrain approach:
if ((_player as Player) is Player)

ristopaasivirta
Автор

That moment when code languages become more human readable.
If <something> is not null almost makes too much sense.

AR-fhuh
Автор

I like that .NET 6 has Nullable property enabled for all projects by default

antonmartyniuk
Автор

0:45 "which is the same as the previous one, but more confusing"
:D thank you for this gem

Everyone using the Unity game engine should stick with "== null", since they overloaded the == operator and they might return null, even if the C# object itself is not null yet.
so using "is null" would skip this check leading to undesired behavior.

Soraphis
Автор

Great video. I haven't kept up with the evolution in the language for a while and this video was nice and concise without any fluff.

MarkOGrady
Автор

As someone who has programmed forever, I hadn't realized this had changed.

ericfleet
Автор

C# finally doing something SQL had pretty much nailed since the begginning 😆
EDIT: I dont mean to bash (pun intended) any language, I work daily with both technologies and I love tem both

AndreaTani
Автор

Visual Basic be like, "You couldn't live with your own failure, where did that bring you? Back to me."

DrewTNaylor
Автор

Thank you so much for these shorts.. can't quite bring myself to watch a longer video, on how to check for null. :)

arithex
Автор

This is why I love C++ pointers. Since a null pointer is 0 you can just check the variable itself as the condition

monawoka
Автор

Damn the overloading just messed it up

jiipoud
Автор

Love these shorts, please keep doing more of this Nick !

LucasSilva-rsyl
Автор

Could you please do a video on how to work with <Nullable>enable</Nullable>

I understand what it is doing and why it is doing it but best practices how to DEAL with it is a mystery for me. When should I initialize them from the ctor, when should I give them a default value, and when should I do a null check in a method?

cookiebot
Автор

Coming from Kotlin as a primary language, now I'm jealous to see the pragmatic way of checking not null in C# using the is not null, I hate to write != null, I know we can write our own extension for this, but it's nice to have in the standard library.

Thanks for sharing 😇

abedfattal
Автор

I have used if (entity is User) - handy if you are dealing with interfaces with multiple implementations and readable

fallingintime
Автор

Shorts King c#👌please put a link to a full video and I am all yours

loryteck
Автор

Yeah and in JIT the HasValue() function is called for every approach! This inclued the == variant.

weluvmusicz
Автор

Won't the usage of "is null" be a cause of bugs in certain situations? The operator might've been overriden to support some behavior. Case at hand: Unity. AFAIK, I heard that null-checking with "is null" can lead to unexpected behavior in there.

As long as that's the case, I disagree with implying that null-checking with "is null" is strictly better, as there might be cases where it doesn't work as expected.

a-minus-b-equals-a
Автор

Soon c# will evolve to fully typed english sentences

smokinjoeee