141

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?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Sergey V
  • 1,683
  • 2
  • 11
  • 15

6 Answers6

172

To enable Nullable Reference Types for all code in a project, add the following to its .csproj file:

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

Alternatively open the Project Properties UI, search for nullable and select the option you want:

enter image description here


To enable this in all projects in the solution, add the property to a Directory.Build.props file instead. You can use such a file to specify other properties across multiple projects too.


If you're targeting a version of .NET earlier than netcoreapp3.0, you'll also need to set LangVersion to 8 or higher, as Nullable Reference Types were added in C# 8:

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

For older Visual Studio versions:

  • You must be using at least VS 16.0
  • In 16.0 preview 1, set NullableReferenceTypes to true.
  • From 16.0 preview 2 to 16.1, set NullableContextOptions to enable.
  • From 16.2 preview 1 onwards, use Nullable as above.
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 9
    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." – Ryan Lundy Aug 03 '19 at 17:27
  • 3
    I also had to update and 16.0 from the older "15,0" before the 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. – Tony Wall Oct 24 '19 at 13:08
  • 1
    @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. – Drew Noakes Oct 31 '19 at 21:18
  • @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. – Tony Wall Jan 06 '20 at 01:58
  • @TonyWall if you have `enable` set in a project, you do not need to add `#nullable enable` to source files in that project. – Drew Noakes Jan 07 '20 at 10:22
  • 1
    @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). – Tony Wall Jan 07 '20 at 15:18
  • @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. – Drew Noakes Jan 07 '20 at 21:50
  • I like the old way, this new feature drives me crazy. All reference types nullable – Moses Sep 28 '22 at 20:09
  • @Moses that's fine, you can disable the feature. But you are giving up on type safety and exposing yourself to more issues at runtime. – Drew Noakes Oct 10 '22 at 03:09
  • Is there a way to use the setting to not allow nulls expcept for classes in a specific folder? What I'm thinking about is properties that model database fields, which can be null, but you don't want null referecne errors on other non database classes. – Paul McCarthy Jun 19 '23 at 13:58
  • 1
    @PaulMcCarthy not that I know of. The best I can think of is to ensure you set the nullable directive in only those files you want it to apply to. – Drew Noakes Jun 20 '23 at 11:51
36

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
  • more information about available values for this option (`enable`, `disable`, `safeonly` etc): https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-reference-types.md#setting-project-level-nullable-context – Sergey V Feb 05 '19 at 17:02
  • 8
    Has this changed again in a recent release? This doesn't seem to work for me in Preview 4.2 – waldrumpus Mar 19 '19 at 10:45
  • 9
    Since Visual studio 6.2 has been simplified to just (see the accepted answer) – Andrew Hill Aug 29 '19 at 04:38
29

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Maxime Gélinas
  • 2,202
  • 2
  • 18
  • 35
24

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+.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ian Yates
  • 921
  • 8
  • 15
14

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.

Sergey V
  • 1,683
  • 2
  • 11
  • 15
  • 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 – Panagiotis Kanavos Dec 05 '18 at 17:39
  • @PanagiotisKanavos, that tag was proposed in comments on YouTube by Mads Torgersen - the author of the video I linked in original question – Sergey V Dec 05 '18 at 17:58
  • 1
    This property isn't yet supported in 'old' projects. Issue is tracked by https://github.com/dotnet/project-system/issues/4058 – Julien Couvreur Dec 12 '18 at 22:04
  • Still nothing happens. – AgentFire Oct 12 '21 at 14:16
10

Legacy csproj format

You asked about the legacy .csproj format. Open up the project file in a text editor and make the following changes:

  1. Add/change <LangVersion>8.0</LangVersion> in the Debug and Release PropertyGroup sections:

     <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <LangVersion>preview</LangVersion>
    
  2. Enable support for nullable reference types by adding <Nullable>enable</Nullable> to the main PropertyGroup:

     <PropertyGroup>
        <Nullable>enable</Nullable>
    

Tested with a .NET WinForms app using C# 8 and nullable reference types syntax in Visual Studio 2019 v16.2.0 Preview 3.


SDK-style project files

SDK style projects are much simpler, and can be edited within Visual Studio. For these all you need is (in the same PropertyGroup as TargetFramework or TargetFrameworks):

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

Notes

  • .NET Core 3.x projects target C# 8 by default, so you won't need to specify the LangVersion for those projects.

  • The default for .NET Framework projects is C# 7.3, and you don't get C# 8.0 even with <LangVersion>latest</LangVersion>. You must explicitly set the language version to 8.0. Please refer to my answer to the question Does C# 8 support the .NET Framework? for more details.

Pang
  • 9,564
  • 146
  • 81
  • 122
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108