แก้ไข

แชร์ผ่าน


Use throw expression (IDE0016)

Property Value
Rule ID IDE0016
Title Use throw expression
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C#
Options csharp_style_throw_expression

Overview

This style rule concerns the use of throw expressions instead of throw statements. Set the severity of rule IDE0016 to define how the rule should be enforced, for example, as a warning or an error.

Options

The associated option for this rule specifies whether you prefer throw expressions or throw statements.

For more information about configuring options, see Option format.

csharp_style_throw_expression

Property Value Description
Option name csharp_style_throw_expression
Option values true Prefer to use throw expressions instead of throw statements
false Prefer to use throw statements instead of throw expressions
Default option value 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;

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

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

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

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

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

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

For more information, see How to suppress code analysis warnings.

See also