How to enable Nullable Reference Types feature of C# 8.0 for the whole project

Sergey V picture Sergey V · Dec 5, 2018 · Viewed 29.4k times · Source

According to the C# 8 announcement video the "nullable reference types" feature can be enabled for the whole project.

But how to enable it for the project? I did not find any new appropriate option in the Project Properties window in Visual Studio 2019 Preview 1.

Can it be enabled for 'legacy' .csproj projects if the C# language version is changed to 8.0?

Answer

Drew Noakes picture Drew Noakes · May 23, 2019

In Visual Studio 16.2 (from preview 1), the property name is changed to Nullable, which is simpler and aligns with the command line argument.

<PropertyGroup>
  ...
  <Nullable>enable</Nullable>
  <LangVersion>8.0</LangVersion>
</PropertyGroup>

Note that if you're targeting netcoreapp3.0 or later, you don't need to specify a LangVersion of 8, as that is the default in .NET Core 3.


For older Visual Studio versions:

  • From 16.0 preview 2 to 16.1, set NullableContextOptions to enable.
  • In 16.0 preview 1, set NullableReferenceTypes to true.