问题描述
根据 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
toenable
- In 16.0 preview 1 set
NullableReferenceTypes
totrue
这篇关于如何为整个项目启用C#8.0的可空引用类型功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!