This is a simple console application to demonstrate Nullable Reference Types, a new feature in .NET Core 3 and C# 8 (both of which are in public preview at the time of writing).
The Nullable Reference Types feature, when enabled, allows you to indicate which reference type variables should be allowed to be null. It then provides some helpful compiler warnings to ensure you're avoiding null where it shouldn't occur and responsibly checking for null where it might occur. The goal is to help you write safer, more expressive code with fewer of those pesky
NullReferenceExceptions we all know and love.
This demo app pretends to retrieve a record from a data store that represents a person, then counts the total number of letters in the person's name and prints the name and letter count to the console. It's the garden-variety nullability example, since a lot of times we don't know someone's middle name. Making the middle name nullable opens up the risk of dereferencing a null value and ending up with a
NullReferenceException. Nullable Reference Types can help us avoid this.
This app was created with the following:
Note that all of these tools are available cross-platform (Windows, Mac, and Linux).
Also note that, because .NET Core 3 and C# 8 are still in preview, the feature may change before it becomes generally available.
Using the .NET Core CLI (installed with the .NET Core SDK), you can build and run the app by navigating to the project directory and invoking
dotnet build and
If you're using the preview release like I did and you're not seeing the compiler warnings you expect, try invoking
dotnet clean followed by
The commits in this repository are intended to help you learn how to take advantage of the Nullable Reference Types feature.
Checking out each commit in order will walk you through the process of creating the app, introducing some bugs that can lead to
NullReferenceExceptions, enabling the Nullable Reference Types feature, and utilizing the compiler warnings it provides to find and fix the bugs. However, checking out commits by hash is a bit cumbersome, so for convenience, there are branches marked with sequential steps that you can check out instead.
I recommend you check out each branch in order (
step-2, etc.). At each step, try to build and run the app (
dotnet build and
dotnet run) to see what happens (yes, I know that
dotnet run automatically builds the project, but the console output of each command is different, which can be instructive). Then inspect what has changed (
git log --oneline -p). And, of course, tinker to your heart's content.
For further reference (pun intended), these are some helpful articles: