C# basics: Different ways to check a null object
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! 👨💻