Microsoft has made available Nullable Reference Types as a preview for developers who want to try the new feature and provide feedback.
The Nullable Reference Type preview comes as a Roslyn extension for Visual Studio 2017 15.5 Preview 4+. .NET Framework projects are supported for now, while support for .NET Core is to follow soon. The feature is enabled by default in the latest C# 8. Microsoft has provided the necessary installation instructions.
The new Nullable Reference Type introduced in C# 8 is meant to solve the Billion Dollar Mistake as Tony Hoare, a British computer scientist, called null references that he invented back in 1965 when working on ALGOL. The main problem with pointers is that sometimes they are null and that is not expected. While introduced as a feature, the null pointer has proved to be a major source of bugs over time.
“Add non-nullable reference types in C#” was suggested on Visual Studio/User Voice back in 2011, and it was pushed up the stack to #15 a year later by users voting. Now it is the #1 feature requested on User Voice. One of the reasons why this feature took so long to be implemented is that C# uses null references everywhere. As Mads Torgersen, lead designer of C#, explained:
The problem is that null references are so useful. In C#, they are the default value of every reference type. What else would the default value be? What other value would a variable have, until you can decide what else to assign to it? What other value could we pave a freshly allocated array of references over with, until you get around to filling it in?
Also, sometimes null is a sensible value in and of itself. Sometimes you want to represent the fact that, say, a field doesn’t have a value. That it’s ok to pass “nothing” for a parameter. The emphasis is on sometimes, though. And herein lies another part of the problem: Languages like C# don’t let you express whether a null right here is a good idea or not.
Instead of introducing non-nullable reference types in C#, Microsoft has chosen to consider reference types as non-nullable by default and provide mechanisms to deal with nullable types. They considered that more often a reference is desired to be non-nullable and to be dereferenced. Torgersen wrote:
- We believe that it is more common to want a reference not to be null. Nullable reference types would be the rarer kind (though we don’t have good data to tell us by how much), so they are the ones that should require a new annotation.
- The language already has a notion of – and a syntax for – nullable value types. The analogy between the two would make the language addition conceptually easier, and linguistically simpler.
- It seems right that you shouldn’t burden yourself or your consumer with cumbersome null values unless you’ve actively decided that you want them. Nulls, not the absence of them, should be the thing that you explicitly have to opt in to.
A nullable reference type is defined with ?
as shown in the example below:
class Person {
public string FirstName; // Not null
public string? MiddleName; // May be null
public string LastName; // Not null
}
Microsoft is asking developers to try this new feature and provide feedback on how it works.
More information on the topic: C# Futures: Nullable Reference Types, C# 8.0 Previewed, A Proposal for Non-Nullable Types in C#.