Note: This post was written a while back but sat in draft. I’ve published this now, but I’m not sure it’s relevant to the latest versions etc. so please bear this in mind.
One of the key C# 8.0 features is nullable/non-nullable reference types, but before we get started you’ll need to enable the language features by editing the csproj and adding the following to each PropertyGroup
<PropertyGroup> <LangVersion>8.0</LangVersion> <Nullable>enable</Nullable> </PropertyGroup>
You can also enable/disable on a per file basis using
#nullable enable
and to disable
#nullable disable
What’s it all about?
So we’ve always been able to assign a null to a reference type (which is also the default value when not initialized), but ofcourse this means if we try to call a method on a null reference we’ll get the good old NullReferenceException. So for example the following (without #nullable enable) will compile quite happily without warnings (although you will see warnings in the Debug output)
string s = null; var l = s.Length;
Now if we add #nullable enable we’ll get the warnings about that we’re attempting to assign a null to a non-nullable. Just like using the ? as a nullable for primitives, for example int? we now mark our reference types with the ?, hence the code now looks like this
string? s = null; var l = s.Length;
In other words we’re saying we expect that the string might be a null. The use of the non-nullable on reference types will hopefully highlight possible issues that may result in NullReferenceExceptions, but as they’re currently warnings you’ll probably want to enable Warnings as Errors.
This is an opt-in feature for obvious reasons i.e. it can have a major impact upon existing projects.
Obviously you still need to handle possible null values. Partly because you might be working with libraries which do not have this nullable reference type option enabled, but also because we can “trick” the compiler – so we know the previously listed code will result in a Dereference of a possibly null reference warning, but what if we change things to the following
public static string GetValue(string s) { return s; } // original code changed to string s = GetValue(null); var l = s.Length;
This still gives us a warning Cannot convert null literal to non-nullable reference type so that’s good but we can change GetValue to this
public static string GetValue([AllowNull] string s) { return s; }
and now, no warnings exist – the point being that even with nullable reference types enabled and not marking a reference type as nullable, we can still get null reference exceptions.
Attributes
As you’ve seen, there’s also (available in .NET core 3.0) some attributes that we can apply to our code to the compiler a little more information about our null expectations. You’ll need to use the following
using System.Diagnostics.CodeAnalysis;
See Update libraries to use nullable reference types and communicate nullable rules to callers for a full list of attributes etc.