Nullable Reference Type in C#

Nullable Reference Type 

   C# is a modern programming language. It is a high level and object oriented language.  It was developed by Microsoft around 2000.  It is 21 year old now but C# language is still being enhanced until today. In 2019, Microsoft release the 8 version of C#. In this version, C# introduce a nullable reference type.  It makes programmer can clearly express the code intention . In this article, I will show you what is nullable reference type feature in C#8. 

Nullable reference vs non nullable reference

Before C#8 , the following syntax does not show any warning at compile time because a reference type's default value is null. 

string a = null; // no compile warning

It is essential that a reference type variable default value is null even you do not make the assignment clear.

string a; // no compile warning message

In C#8 version, it separates the reference type to nullable or non nullable.  For nonNullable variable when you assign a null value or null variable to it. The compiler give you a warning message but nullable types does not. The following two variables are nullable and non nullable variables.

string? nullable = null;
string nonNullable = null; // warning message

nonNullable = nullable; // warning message

Before you experience this new feature, you have to add the following element to project file(.csproj) under <PropertyGroup> element because C# 8 default is disabled this breaking change feature.

After that, the visual studio code(vc) will prompt an alert message when you mouse over the variable.

In addition, the compiler gives you the following message at compile time for nonNullable variable.

In C# 8 when you enable the nullable feature, all reference type variable are non nullable unless you appended ? before variable name like that. 

string? nullable = null; // non compile warning message

This variable is nullable. Compiler does not give you a warning message at compile time when you assign a null value or null variable to this variable. 

Another situation is that when the variable is nullable and assign a null value to it. Once you try to do the following operation, vc and compiler give you a warning message too. 

The static code analysis help you to identify the NullReferenceException Risk in compile time. If you are building a large library or software system. This new feature is very helpful. 

Besides ? , ! operator is another useful thing in nullable reference type manipulation. The compiler give a warning message about null reference to the following code.

C#8 compiler detects that this variable may be null and you may access a null value  you a warning message that the NullReferenceException may appears in runtime. Sometime, you may think that this message is annoying and you want to remove it without affecting the interpretation of nullability. In this situation, ! operator do a big help to you. 

!(null-forgiving) operator 

C# 8 introduced  !(null-forgiving) operator to express the variable cannot be null. Programmers use this operator to tell the compiler that we know this variable never be null value and you should skip to checking it.  For the above example, once you put the operation before the . symbol. The compiler warning message disappears. 

Setting

Although C# 8 introduces this feature, but it is not default enable. In order to enable this feature, you have to set the Nullable element in ".csproj" file. 

Element: <Nullable>enable</Nullable>

This element configures the compiler to checking the types of nullability and what kind of warning should be generated.  There are four value you can set 

1. enable: The nullable annotation context is enabled. The nullable warning context is enabled.

2. warnings: The nullable annotation context is disabled. The nullable warning context is enabled.

3. annotations: The nullable annotation context is enabled. The nullable warning context is disabled.

4. disabled: The nullable annotation context is disabled. The nullable warning context is disabled.

If you want to find more useful information about those setting. You can go to https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references page to study or search nullable context on internet. But I will not mention more in this article because in real case, the most frequent usage is enable and disabled setting. 

Conclusion

Checking value is null at compile time enhances the code readability, avoid NullReferenceException at runtime and can let the other programmer understand your code intention.  Although you may think this breaking change is annoying. It forces you to coding in a new way. But all in all,  the benefit is greater than the hardship at the beginning especially if you are framework programmer. Please think about to use it in your new project. It helps you to find out the potential bug without overwhelming you.  And Microsoft also think about the migration problem. The warning message is ignored if you apply a new library which implemented in C#8 but use it in the previous version. Once you upgrade the old one and enable this feature, the warning messages shows immediately.  

Get the latest article from lab.

Please leave your email below.

Subscribe

Thank you for you subscription