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

56,284

Solution 1

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.

Add the following properties to your .csproj file.

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

If you're targeting netcoreapp3.0 or later, you don't need to specify a LangVersion to enable nullable reference types.


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.

Solution 2

Note that this setting is changed between VS 2019 preview 1 and preview 2. With preview 2 or 3, you need this in your .csproj:

<PropertyGroup>
  <LangVersion>8.0</LangVersion>
  <NullableContextOptions>enable</NullableContextOptions>
</PropertyGroup>

The <NullableReferenceTypes> mentioned in the earlier answer (which, when I originally wrote this answer on 4th Feb 2019, had been marked as the accepted answer) was correct at the time that answer was written, but it is no longer recognized.

Solution 3

In addition to @DrewNoakes accepted answer, note that the nullable property can be set for all projects at once by adding a file called Directory.Build.props in the folder that contains your .sln file.

Just define your Directory.Build.props file like this:

<Project>

  <PropertyGroup>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

You will need to restart Visual Studio for this to take effect.

More about Directory.Build.props.

Solution 4

Worth noting that, by now, this is also an exposed setting in a project's Properties page:

"Build" tab shows "Nullable" setting

At least in VS2019 16.6+.

Solution 5

For Visual Studio 2019 Preview 2 & 3, see Ian Griffiths's answer.

Solution for Visual Studio 2019 Preview 1:

To enable Nullable Reference Types feature for the .NET Core project, add NullableReferenceTypes property to the .csproj file like this:

<PropertyGroup>
  ...
  <NullableReferenceTypes>true</NullableReferenceTypes>
  <LangVersion>8.0</LangVersion>
</PropertyGroup>

As @JulienCouvreur referenced in comments regarding to https://github.com/dotnet/project-system/issues/4058, the new property is not yet supported in 'old' project system, but will be supported before C# 8.0 released.

Share:
56,284
Sergey V
Author by

Sergey V

Updated on November 04, 2021

Comments

  • Sergey V
    Sergey V over 2 years

    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?

  • Panagiotis Kanavos
    Panagiotis Kanavos over 5 years
    Have you tried changing the target framework to net472 ? How/where did you find that setting by the way? That reference would be very useful. I found many things that don't quite work as shown the video
  • Sergey V
    Sergey V over 5 years
    @PanagiotisKanavos, that tag was proposed in comments on YouTube by Mads Torgersen - the author of the video I linked in original question
  • Julien Couvreur
    Julien Couvreur over 5 years
    This property isn't yet supported in 'old' projects. Issue is tracked by github.com/dotnet/project-system/issues/4058
  • Sergey V
    Sergey V over 5 years
    more information about available values for this option (enable, disable, safeonly etc): github.com/dotnet/roslyn/blob/master/docs/features/…
  • waldrumpus
    waldrumpus over 5 years
    Has this changed again in a recent release? This doesn't seem to work for me in Preview 4.2
  • Larry Smith
    Larry Smith about 5 years
    I just tried this with VS 2019/ 16.1.1 and it didn't appear to do anything. To test it, I then went and put in #nullable enable in one of the classes and then I could see the warnings about null objects.
  • Drew Noakes
    Drew Noakes about 5 years
    @LarrySmith apologies, this change was made in 16.2P1. I've updated my answer. In 16.1.1 you'll need NullableContextOptions still.
  • andriy
    andriy almost 5 years
    Note that the boolean logic might be a bit non-intuitive: enable means "enable the new C# 8.0 setting where types are non-nullable by default". disable means "do it the old way where every type is nullable."
  • Andrew Hill
    Andrew Hill almost 5 years
    Since Visual studio 6.2 <NullableContextOptions> has been simplified to just <Nullable> (see the accepted answer)
  • Tony Wall
    Tony Wall over 4 years
    I also had to update <Project ToolsVersion="16.0" ...> and <VisualStudioVersion ...>16.0</VisualStudioVersion> from the older "15,0" before the <Nullable> would work on older non-SDK projects, even though they were correctly upgraded to framework 4.8 via the properties GUI of VS 16.3 RTM. Only the C#8 language version was respected without any additional project file editing.
  • Drew Noakes
    Drew Noakes over 4 years
    @TonyWall I'm curious why you needed that. I just created a new .NET Framework Console App in VS 16.3.7 (i.e. non-SDK style project), added LangVersion and Nullable properties to the .csproj and it works fine. The project has ToolsVersion="15.0" too.
  • Tony Wall
    Tony Wall over 4 years
    @DrewNoakes the problems come if you enable best practice code analysis and gets deeper. Even with these manual properties set the hard way the collection of tools currently used doesn't respect these new "standard" properties. MS need to align this and encourage the open source parts to come into line before the next RTM. VS is "Visual" Studio and we shouildn't have to mess about like this. In practice currently it is still necessary to add "#nullable enable" to the top of each file so this answer is not entirely valid after further experience, just the C# version part. Shame.
  • Drew Noakes
    Drew Noakes over 4 years
    @TonyWall if you have <Nullable>enable</Nullable> set in a project, you do not need to add #nullable enable to source files in that project.
  • Tony Wall
    Tony Wall over 4 years
    @DrewNoakes No, as I said Nullable doesn't always work, especially if you use best practices like the new Code Analysis NuGet packages and highest warning levels / treat warnings as errors (maybe you missed some warnings/messages and didn't notice the problem still exists).
  • Drew Noakes
    Drew Noakes over 4 years
    @TonyWall I do use those packages (I help to develop them in fact) and TreatWarningsAsErrors on several projects. My comment still holds, if you set it in the project, it is not needed in source. If you're experiencing that, then there is some other problem, potentially a bug in an analyzer. Please open a feedback ticket in Visual Studio with a repro. If you link it here I will have a look.
  • AyCe
    AyCe about 3 years
    But not for old-style csproj.
  • AgentFire
    AgentFire over 2 years
    I don't see that option.
  • AgentFire
    AgentFire over 2 years
    Nope, still nothing.
  • AgentFire
    AgentFire over 2 years
    Still nothing happens.