Visual Studio 2019 16.2.0 Preview 1, C# 8.0 and Nullable

May 23, 2019  

I’ve been gradually switching my projects to use the new nullable reference types feature of C# 8.0, which required me to add following snippet to the *.csproj files:

1<PropertyGroup>
2  <LangVersion>preview</LangVersion>
3  <NullableContextOption>enable</NullableContextOption>
4</PropertyGroup>

Visual Studio 2019 16.2.0 Preview 1, installed on May 22 2019, stopped accepting the nullable reference types with these warnings:

CS8632: The annotation for nullable reference types should only be used in code within a ‘#nullable’ context.

and also:

CS8627: A nullable type parameter must be known to be a value-type or non-nullable reference type. Consider adding a ‘class’, ‘struct’ or type constraint.

Multiple people reported this issue on developercommunity.visualstudio.com.

Solution

This has been confirmed by Microsoft as being the expected behaviour. The <NullableContextOption> settings has been replaced with the shorted <Nullable>:

1<PropertyGroup>
2  <LangVersion>preview</LangVersion>
3  <Nullable>enable</Nullable>
4</PropertyGroup>

As in my case I am configuring all projects through a central Dîrectory.Build.props file, I had to close and reopen the solution for Visual Studio 2019 to pick up the change.

Directory.Build.props

Here is the Directory.Build.props file I use:

1<Project>
2  <PropertyGroup>
3    <!-- Enable nullable reference types for all C# projects -->
4    <LangVersion>preview</LangVersion>
5    <Nullable>enable</Nullable>
6  </PropertyGroup>
7</Project>