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:
<PropertyGroup>
<LangVersion>preview</LangVersion>
<NullableContextOption>enable</NullableContextOption>
</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>
:
<PropertyGroup>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
</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:
<Project>
<PropertyGroup>
<!-- Enable nullable reference types for all C# projects -->
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>