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

6

154

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?

Grim answered 5/12, 2018 at 13:33 Comment(0)
S
189

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.
Sadiras answered 23/5, 2019 at 3:7 Comment(12)
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."Giacopo
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.Sigh
@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.Sadiras
@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.Sigh
@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.Sadiras
@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).Sigh
@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.Sadiras
I like the old way, this new feature drives me crazy. All reference types nullableNimbostratus
@Nimbostratus that's fine, you can disable the feature. But you are giving up on type safety and exposing yourself to more issues at runtime.Sadiras
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.Barbarous
@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.Sadiras
I'm working with Visual Studio Enterprise 2022, version 17.9.6. Is there no way to do this using the Visual Studio IDE? (I prefer not to alter the *.csproj file manually)Uralian
M
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.

Marsupium answered 4/2, 2019 at 17:38 Comment(3)
more information about available values for this option (enable, disable, safeonly etc): github.com/dotnet/roslyn/blob/master/docs/features/…Grim
Has this changed again in a recent release? This doesn't seem to work for me in Preview 4.2Stomatology
Since Visual studio 6.2 <NullableContextOptions> has been simplified to just <Nullable> (see the accepted answer)Kern
G
31

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.

Gliadin answered 29/12, 2019 at 20:24 Comment(0)
B
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+.

Baronial answered 4/11, 2020 at 8:27 Comment(3)
But not for old-style csproj.Myungmyxedema
I don't see that option.Talie
This UI was only available in VS 2019. It changed in VS 2022 to the one shown in my answer.Sadiras
G
15

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.

Grim answered 5/12, 2018 at 15:42 Comment(4)
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 videoUppermost
@PanagiotisKanavos, that tag was proposed in comments on YouTube by Mads Torgersen - the author of the video I linked in original questionGrim
This property isn't yet supported in 'old' projects. Issue is tracked by github.com/dotnet/project-system/issues/4058Ephialtes
Still nothing happens.Talie
T
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.

Tauten answered 14/7, 2019 at 1:47 Comment(1)
Nope, still nothing.Talie

© 2022 - 2024 — McMap. All rights reserved.