gpt4 book ai didi

c# - 可为 null 的引用类型的注释只能在 '#nullable' 上下文中的代码中使用

转载 作者:可可西里 更新时间:2023-11-01 07:54:59 25 4
gpt4 key购买 nike

我有一个控制台应用程序来试用 C# 8 空引用类型。将项目切换为使用语言版本 C# 8 构建。

然后以下代码会导致警告。

 class Program
{
static void Main(string[] args)
{
string? message = "Hello World";
string message2 = null;

Console.WriteLine(message);
Console.WriteLine(message2);

// The annotation for nullable reference types should only be used in code within a '#nullable' context
}
}

这到底是什么意思?

最佳答案

致所有来到这里的人。你可以把#nullable enable按照@Marc 在评论中的建议,在文件之上进行逐个文件的方法。

您还可以使用#nullable enable/disable 的组合来注释文件的一部分

class Program
{
static void Main(string[] args)
{
#nullable enable
string? message = "Hello World";
#nullable disable
string message2 = null;

Console.WriteLine(message);
Console.WriteLine(message2);
}
}

这是文档的链接。 https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts

Nullable contexts enable fine-grained control for how the compiler interprets reference type variables. The nullable annotation context of any given source line is either enabled or disabled. You can think of the pre-C# 8.0 compiler as compiling all your code in a disabled nullable context: any reference type may be null. The nullable warnings context may also be enabled or disabled. The nullable warnings context specifies the warnings generated by the compiler using its flow analysis.

The nullable annotation context and nullable warning context can be set for a project using the Nullable element in your .csproj file. This element configures how the compiler interprets the nullability of types and what warnings are generated. Valid settings are:

  • enable:
    • The nullable annotation context is enabled. The nullable warning context is enabled.
    • Variables of a reference type, string for example, are non-nullable. All nullability warnings are enabled.
  • warnings:
    • The nullable annotation context is disabled. The nullable warning context is enabled.
    • Variables of a reference type are oblivious. All nullability warnings are enabled.
  • annotations:
    • The nullable annotation context is enabled. The nullable warning context is disabled.
    • Variables of a reference type, string for example, are non-nullable. All nullability warnings are disabled.
  • disable:
    • The nullable annotation context is disabled. The nullable warning context is disabled.
    • Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.

在你的.csproj文件,只需添加 <Nullable>enable</Nullable>在相关<PropertyGroup>元素(您的项目文件可能有单独的 <PropertyGroup> 元素用于每个项目配置名称)。

所以你的项目文件应该是这样的:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

要将可为 null 的消息显示为错误而不是警告,请将其添加到您的项目文件中:

<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>

...像这样:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
</PropertyGroup>

</Project>

对应的完整消息是:

  • CS8600:将 null 文字或可能的 null 值转换为不可为 null 的类型。
  • CS8602:可能取消对空引用的引用。
  • CS8603:可能返回空引用。

You can also use directives to set these same contexts anywhere in your project:

  • #nullable enable: Sets the nullable annotation context and nullable warning context to enabled.
  • #nullable disable: Sets the nullable annotation context and nullable warning context to disabled.
  • #nullable restore: Restores the nullable annotation context and nullable warning context to the project settings.
  • #nullable disable warnings: Set the nullable warning context to disabled.
  • #nullable enable warnings: Set the nullable warning context to enabled.
  • #nullable restore warnings: Restores the nullable warning context to the project settings.
  • #nullable disable annotations: Set the nullable annotation context to disabled.
  • #nullable enable annotations: Set the nullable annotation context to enabled.
  • #nullable restore annotations: Restores the annotation warning context to the project settings.

By default, nullable annotation and warning contexts are disabled. That means that your existing code compiles without changes and without generating any new warnings.

请注意,C# 8.0 和 Visual Studio 2019 的预发布版本也支持 safeonly , 然而this option has since been removed并且不存在于最终发布的 C# 8.0 中。此外,预发布版本使用了 #pragma warning restore nullable但发布的版本使用 #nullable restore warnings .

关于c# - 可为 null 的引用类型的注释只能在 '#nullable' 上下文中的代码中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com