使用模式匹配(not 运算符)(IDE0083)

属性
规则 ID IDE0083
标题 使用模式匹配(not 运算符)
类别 Style
Subcategory 语言规则(模式匹配首选项)
适用的语言 C# 9.0+
选项 csharp_style_prefer_not_pattern

概述

在可能的情况下,此样式规则与是否使用 C# 9.0 not 模式有关。

选项

选项指定你希望规则强制实施的行为。 若要了解如何配置选项,请参阅选项格式

csharp_style_prefer_not_pattern

属性 说明
选项名称 csharp_style_prefer_not_pattern
选项值 true 最好尽可能使用 not 模式
false 宁愿不使用 not 模式。
默认选项值 true

注意

当此选项设置为 false 时,分析器不会标记 not 模式的使用。 但是,生成的任何代码都不会使用 not 模式。 当此选项设置为 true 时,会标记不使用 not 模式的代码,并且生成的任何代码都将使用 not 模式(如果适用)。

以下示例演示了当此选项设置为 truefalse 时,代码生成功能将如何生成代码。

// csharp_style_prefer_not_pattern = true
var y = o is not C c;

// csharp_style_prefer_not_pattern = false
var y = !(o is C c);

抑制警告

如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。

#pragma warning disable IDE0083
// The code that's violating the rule is on this line.
#pragma warning restore IDE0083

若要对文件、文件夹或项目禁用该规则,请在配置文件中将其严重性设置为 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0083.severity = none

若要禁用所有代码样式规则,请在配置文件中将类别 Style 的严重性设置为 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

有关详细信息,请参阅如何禁止显示代码分析警告

另请参阅