What's Coming in C# 8.0? Nullable Reference Types

Before I go any further, I would like to point out that as I'm writing this, the feature-set for C# 8.0 still hasn't been decided. This means that information written here is subject to change.

One of the features being discussed for introduction in C# 8.0 is Nullable Reference Types. A proficient C# Developer might say "What?! Aren't all reference types nullable?" It's true, in C#, unlike other languages such as F#, null is 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.

Yet!

How?

From C# 8.0, all reference types will be considered to be non-nullable by default. When you want a nullable reference type you will have to express that explicitly. Yes, a consequence of this design choice is that this will add new warnings or errors to existing code!

Here is what a sample BlogPost class will look like in C# 8.0:

class BlogPost
{
    string title;   // Definitely not null
    string? shortDescription; // May be null
    string body;    // Definitely not null
    
    void test()
    {
        title = null; // WARNING! Cannot convert null to non-nullable reference
        shortDescription = null; // OK
        var length = shortDescription.Length; // Warning: Possible dereference of a null reference
        if(shortDescription != null) 
        { 
            var length = shortDescription.Length; // OK, you checked 
        }
    }
}

This means that this class is now able to express the intent that every blog post has a Title and Body but only some blog posts have a ShortDescription. In fact, this is why this language feature is called "nullable reference types". Those are the ones that are being added to the language. The nonnullable ones are already there, at least syntactically.

The fact that this is a breaking change, is by design. Microsoft want it to complain about your existing code because part of the purpose of this feature is to find bugs in existing code. Having said that, the C# Design Team have implemented these in such a way to make developer's life easier. Firstly, null behavior will be in the form of warnings, not errors and secondly, there's going to be a compiler switch to turn these new warnings on or off.

Why?

So, now that we know how this new feature works, one my ask "Why does the C# Design Team feel the need to introduce this?"

The reason is the terrifying NullReferenceException! We've all run into this exception plenty of times and they are mistakes that could have easily been caught at compile time if only we had the means to express it. Nullable reference types don't solve this problem, but they do allow us to express our intent much better. This is the only way to force developers be aware that a reference type may be nullable or cannot be nullable.

Try it out!

If you would like to try out this feature before it is even released, I suggest you read The C# Nullable Reference Types Preview on C# Language GitHub. In there you will find Installation (and Uninstallation Instructions) as well as the ability to give feedback to the C# Design Team.