Nullable reference types

C# now includes the concept of a “nullable” reference type. The name is a bit confusing, because reference type variables were always nullable. What they really should have called it is “non-nullable”, since that’s what’s different now.

In the past, you could declare a string variable or property, and it was perfectly valid to assign that to null. So if you’re going to use it, you need to do a null-check to avoid exceptions.

Now, if you turn on the flag in your project, the compiler will help you avoid null exceptions, by forcing you to define when a variable or property is allowed to be null, and there are warnings or errors to alert you when you’re trying to potentially set a non-nullable variable to null.

View code on GitHub

Leave a Reply

Your email address will not be published.