使用 throw 表达式 (IDE0016)

财产 价值
规则 ID IDE0016
标题 使用 throw 表达式
类别 样式
子类别 语言规则(表达式级首选项)
适用的语言 C#
选项 csharp_style_throw_expression

概述

此样式规则涉及使用 throw 表达式而非 throw 语句。 设置规则 IDE0016 的严重程度,以定义应如何强制实施该规则,例如作为警告或错误。

选项

此规则的关联选项指定是首选 throw 表达式还是 throw 语句。

有关配置选项的详细信息,请参阅 选项格式

csharp_style_throw_expression

财产 价值 描述
选项名称 csharp_style_throw_expression
选项值 true 首选使用 throw 表达式而不是 throw 语句
false 首选使用 throw 语句而不是 throw 表达式
默认选项值 true
// csharp_style_throw_expression = true
this.s = s ?? throw new ArgumentNullException(nameof(s));

// csharp_style_throw_expression = false
if (s == null) { throw new ArgumentNullException(nameof(s)); }
this.s = s;

禁止显示警告

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

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

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

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

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

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

有关详细信息,请参阅 如何取消代码分析警告

另请参阅