Nullable reference types? What’s that?
The hottest novelty of C# 8 is almost as hot as gossips about Bradley Cooper and Lady Gaga affair… Or maybe nullable reference types are hotter? Hard to say. Never mind, I wrote about it once when everything was just an assumption. Now, we finally can turn this feature on and dive into some details 😊.
How to turn it on?
When you open your project in Visual Studio 2019, nothing is changed. Your code will work like it used to. Even when you create a brand-new project in VS 2019, reference types will still be capable to keep null or non-null values.
When you decide to turn this feature on, you have to open .csproj file (ex. by clicking project’s name twice in Solution Explorer) and add a node in the xml. It can have one of the below values:
- disable – everything will work like it used to. It’s the default value right now.
- enable – we turn on the new feature, all the reference types will be non-nullable. You will get warnings (not errors!) when you try the illegal assignment.
I only mentioned the last 2 options because I don’t use them :D. But if you are really curious what other values mean, grab a link: https://docs.microsoft.com/pl-pl/dotnet/csharp/nullable-references
What if I always ignore compiler’s warnings?
Ha! No worries, you can always make compiler to treat warnings as errors. To do so, just add the below xml node in you .csproj file:
So, to sum up, my basic .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <nullable>enable</nullable> <WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors> </PropertyGroup> </Project>
Aaaand… that’s not all what we can do!
We just turned nullable reference types globally, for the whole project. But what if we want to refactor our huge project, introducing this new feature, file after file? Yeah, we can do it! We can even make only a part of .cs file to accept this new feature and the other part work like it used to. I can’t decide if it is more amazing or scary 😛.
To do so, just add the below line in any place in your code:
Of course, you can put as many #nullable in your code as you need.
Now, when you know everything, take a look at the screenshots below.
This is how VS works, when we turn on the nullable reference types globally and switch it off in source code:
And that how it works, when we turn this feature globally and order VS to treat warnings as errors:
And at the very end, the above example with nullable reference types turned off in the middle of the code:
As you can see, the error from line 19 is gone.
What if I can’t use .net core 3.0?
No drama, I’m coding-llama! You can turn this feature on even in .net core 1.0 (!!) projects. To do so, open you .csproj file and add the below two lines:
As you can see, we have to additionally set language version to 8.0. And that’s it, the whole .csproj file will look like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> <LangVersion>8.0</LangVersion> <nullable>enable</nullable> <WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors> </PropertyGroup> </Project>