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

扶醉桌前 提交于 2019-11-27 12:41:31

问题


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?


回答1:


In Visual Studio 16.2 (from preview 1) the property name 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, then 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



回答2:


Note that this setting 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>

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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/53633538/how-to-enable-nullable-reference-types-feature-of-c-sharp-8-0-for-the-whole-proj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!