Skip to content
A simple console app to demonstrate Nullable Reference Types using .NET Core 3 and C# 8
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitattributes
.gitignore
LICENSE
NullableReferenceTypesDemo.csproj
Program.cs
README.md

README.md

Overview

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.

Setup

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.

Run

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 dotnet run.

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 dotnet build.

Learn

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-1, 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:

You can’t perform that action at this time.