C# basics: Different ways to check a null object

SinghSharp
1 min readAug 21, 2021

--

C# provides various ways to check for a null object. Let’s look at some of the ways we can achieve that:

The most conventional way to check for null is by equating the object with null. I am initializing the null object in my example but you can change that as per requirement.

While the above code works fine in almost all the scenarios, in C#7 we got a new way to write the above code in a more readable way by using the is keyword.

That’s not all we can write the same code using null-coalescing operator.

In C#9, Microsoft introduced a new way to check if the object is not null by using (you guessed it right) is not keyword.

Do you know any other way we could check for null? Let me know in the comments below. Till then, Happy Coding! 👨‍💻

Also, buy me a coffee, book, or a pizza by clicking the link below if you liked my work and want to support me. 😅

https://www.buymeacoffee.com/manmohit

--

--

SinghSharp
SinghSharp

Written by SinghSharp

A full-stack web developer passionate about software development, science and technology.

Responses (2)