November 2017
Intermediate to advanced
800 pages
30h 51m
English
Checking whether a reference type or nullable value type variable currently contains null is important because if you do not, a NullReferenceException can be thrown, causing an error in your code:
// check is myVariable is not null before using it
if (ICouldBeNull != null)
{
// do something with ICouldBeNull
}
If you are trying to get a field or property from a variable that might be null, use the null check operator (?.), as shown in the following code:
string authorName = null; // if authorName is null, instead of throwing an exception, // null is returned int? howManyLetters = authorName?.Length;
Sometimes you want to either assign a variable to a result, or use an alternative value, such as zero, if the variable is ...
Read now
Unlock full access