Редагувати

Поділитися через


Add braces (IDE0011)

Property Value
Rule ID IDE0011
Title Add braces
Category Style
Subcategory Language rules (code-block preferences)
Applicable languages C#
Options csharp_prefer_braces

Overview

This style rule concerns the use of curly braces { } to surround code blocks.

Options

Use the following option to specify whether curly braces are preferred or not, and if preferred, whether only for multi-line code blocks.

For more information about configuring options, see Option format.

csharp_prefer_braces

Property Value Description
Option name csharp_prefer_braces
Option values true Prefer curly braces even for one line of code
false Prefer no curly braces if allowed
when_multiline Prefer curly braces on multiple lines
Default option value true
// csharp_prefer_braces = true
if (test) { this.Display(); }

// csharp_prefer_braces = false
if (test) this.Display();

// csharp_prefer_braces = when_multiline
if (test) this.Display();
else { this.Display(); Console.WriteLine("Multiline"); }

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 IDE0011
// The code that's violating the rule is on this line.
#pragma warning restore IDE0011

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

[*.{cs,vb}]
dotnet_diagnostic.IDE0011.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