如何为整个项目启用C

如何为整个项目启用C

本文介绍了如何为整个项目启用C#8.0的可空引用类型功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 C#8公告视频,可以为整个项目.

According to the C# 8 announcement video the "nullable reference types" feature can be enabled for the whole project.

但是如何为项目启用它?我没有发现在项目属性窗口中的任何新的合适的选项在Visual Studio 2019预览1.

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.

如果将C#语言版本更改为8.0,可以为传统" .csproj项目启用该功能吗?

Can it be enabled for 'legacy' .csproj projects if the C# language version is changed to 8.0?

推荐答案

在Visual Studio 16.2中(从预览1开始),属性名称更改为Nullable,这更简单并且与命令行参数对齐.

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>

请注意,如果您要定位netcoreapp3.0或更高版本,则无需指定LangVersion为8,因为这是.NET Core 3中的默认设置.

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.

对于旧版Visual Studio:

For older Visual Studio versions:

  • 从16.0预览2到16.1,将NullableContextOptions设置为enable
  • 在16.0预览版1中,将NullableReferenceTypes设置为true
  • From 16.0 preview 2 to 16.1 set NullableContextOptions to enable
  • In 16.0 preview 1 set NullableReferenceTypes to true

这篇关于如何为整个项目启用C#8.0的可空引用类型功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:00