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

前端 未结 6 1811
庸人自扰
庸人自扰 2020-12-05 03:44

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 n

6条回答
  •  爱一瞬间的悲伤
    2020-12-05 04:30

    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 8.0 in the Debug and Release PropertyGroup sections:

       
          preview
      
    2. Enable support for nullable reference types by adding enable to the main PropertyGroup:

       
          enable
      

    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):

      
        8.0
        enable
      
    

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

提交回复
热议问题