Редагувати

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


Language and unnecessary rules

Code-style language rules affect how various constructs of .NET programming languages, for example, modifiers, and parentheses, are used.

This category also includes rules that identify parts of the code base that are unnecessary and can be refactored or removed. The presence of unnecessary code indicates one of more of the following problems:

  • Readability: Code that unnecessarily degrades readability.
  • Maintainability: Code that's no longer used after refactoring and is maintained unnecessarily.
  • Performance: Unnecessary computation that has no side effects and leads to unnecessary performance overhead.
  • Functionality: Functional issue in code that makes required code redundant. For example, IDE0060 flags unused parameters where the method accidentally ignores an input parameter.

The language rules fall into the following categories:

  • .NET style rules: Rules that apply to both C# and Visual Basic. The option names for these rules start with the prefix dotnet_style_.
  • C# style rules: Rules that are specific to C# code. The option names for these rules start with the prefix csharp_style_.
  • Visual Basic style rules: Rules that are specific to Visual Basic code. The option names for these rules start with the prefix visual_basic_style_.

Option format

Many of the language rules have one or more associated options to customize the preferred style. For example, Use simple 'using' statement (IDE0063) has the associated option csharp_prefer_simple_using_statement that lets you define whether you prefer a using declaration or a using statement. The rule enforces whichever options you choose at a specified level, for example, warning or error.

Options for language rules can be specified in a configuration file with the following format:

option_name = value (Visual Studio 2019 and later)

or

option_name = value:severity

  • Value

    For each language rule, you specify a value that defines if or when to prefer the style. Many rules accept a value of true (prefer this style) or false (do not prefer this style). Other rules accept values such as when_on_single_line or never.

  • Severity (optional in Visual Studio 2019 and later versions)

    The second part of the rule specifies the severity level for the rule. In .NET 9 and later versions, the severity is always respected—that is, inside development IDEs and during command-line builds. In .NET 8 and earlier versions, this severity setting is only respected inside development IDEs, such as Visual Studio, and not during build.

    If you're using the .NET 8 SDK or an earlier version and you want the severity to be respected at build time, you can do so in one of two ways:

    • Set the <AnalysisLevel> or <AnalysisLevelStyle> property to 9.0 or higher, or to preview.
    • Set the severity by using the rule ID-based severity configuration syntax for analyzers instead. The syntax takes the form dotnet_diagnostic.<rule ID>.severity = <severity>, for example, dotnet_diagnostic.IDE0040.severity = warning. For more information, see severity level.

Tip

Starting in Visual Studio 2019, you can configure code style rules from the Quick Actions light bulb menu after a style violation occurs.

Rule index

Language and unnecessary code rules are further categorized into subcategories, such as expression-level preferences, code-block preferences, and modifier preferences.

using directive preferences

.NET style rules (C# and Visual Basic):

C# style rules:

Code-block preferences

C# style rules:

Expression-bodied members

C# style rules:

Expression-level preferences

.NET style rules (C# and Visual Basic):

C# style rules:

Visual Basic style rules:

Field preferences

.NET style rules (C# and Visual Basic):

Language keyword vs. framework types preferences

.NET style rules (C# and Visual Basic):

Modifier preferences

.NET style rules (C# and Visual Basic):

C# style rules:

New-line preferences

  • Allow multiple blank lines (IDE2000)
  • Allow embedded statements on same line (IDE2001)
  • Allow blank lines between consecutive braces (IDE2002)
  • Allow statement immediately after block (IDE2003)
  • Allow blank line after colon in constructor initializer (IDE2004)
  • Allow blank line after token in conditional expression (IDE2005)
  • Allow blank line after token in arrow expression (IDE2006)

Null-checking preferences

C# style rules:

Parameter preferences

.NET style rules (C# and Visual Basic):

Parentheses preferences

.NET style rules (C# and Visual Basic):

Pattern-matching preferences

C# style rules:

Suppression preferences

.NET style rules (C# and Visual Basic):

This. and me. preferences

.NET style rules (C# and Visual Basic):

var preferences

C# style rules:

See also