Checking for Null Values
Learn how to handle null values in C# by checking for null in nullable reference type and nullable value type variables and use operators.
Checking whether a nullable reference type or nullable value type variable currently contains null is important because if we do not, a NullReferenceException
can be thrown, which results in an error.
Handling null values
We should check for a null value before using a nullable variable, as shown in the following code:
Press + to interact
// check that the variable is not null before using itif (thisCouldBeNull != null){// access a member of thisCouldBeNullint length = thisCouldBeNull.Length; // could throw exception...}
C# 7 introduced is combined with the !
(not) operator as an alternative to !=
, as shown in the ...
Access this course and 1400+ top-rated courses and projects.