Language rules
Code-style language rules affect how various constructs of .NET programming languages, for example, modifiers, and parentheses, are used. The 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
Options for language rules can be specified in a configuration file with the following format:
option_name = value
(Visual Studio 2019 version 16.9 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) orfalse
(do not prefer this style). Other rules accept values such aswhen_on_single_line
ornever
.Severity (optional in Visual Studio 2019 version 16.9 and later versions)
The second part of the rule specifies the severity level for the rule. When specified in this way, the severity setting is only respected inside development IDEs, such as Visual Studio. It is not respected during build.
To enforce code style rules at build time, 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 = none
. For more information, see severity level.
Tip
Starting in Visual Studio 2019 version 16.3, you can configure code style rules from the Quick Actions light bulb menu after a style violation occurs. For more information, see Automatically configure code styles in Visual Studio.
.NET style rules
The style rules in this section are applicable to both C# and Visual Basic.
- 'this.' and 'Me.' qualifiers
- Language keywords instead of framework type names for type references
- Modifier preferences
- Parentheses preferences
- Expression-level preferences
- Use object initializers (IDE0017)
- Use collection initializers (IDE0028)
- Use auto-implemented property (IDE0032)
- Use explicitly provided tuple name (IDE0033)
- Use inferred member names (IDE0037)
- Use conditional expression for assignment (IDE0045)
- Use conditional expression for return (IDE0046)
- Use compound assignment (IDE0054 and IDE0074)
- Simplify interpolation (IDE0071)
- Simplify conditional expression (IDE0075)
- Add missing cases to switch statement (IDE0010)
- Convert anonymous type to tuple (IDE0050)
- Use 'System.HashCode.Combine' (IDE0070)
- Convert
typeof
tonameof
(IDE0082)
- Null-checking preferences
- File header preferences
- Namespace naming preferences (IDE0130)
C# style rules
The style rules in this section are applicable to C# language only.
- 'var' preferences (IDE0007 and IDE0008)
- Expression-bodied members
- Use expression body for constructors (IDE0021)
- Use expression body for methods (IDE0022)
- Use expression body for operators (IDE0023 and IDE0024)
- Use expression body for properties (IDE0025)
- Use expression body for indexers (IDE0026)
- Use expression body for accessors (IDE0027)
- Use expression body for lambdas (IDE0053)
- Use expression body for local functions (IDE0061)
- Pattern matching preferences
- Expression-level preferences
- Inline variable declaration (IDE0018)
- Simplify 'default' expression (IDE0034)
- Use local function instead of lambda (IDE0039)
- Deconstruct variable declaration (IDE0042)
- Use index operator (IDE0056)
- Use range operator (IDE0057)
- Simplify
new
expression (IDE0090) - Add missing cases to switch expression (IDE0072)
- Use tuple to swap values (IDE0180)
- "Null" checking preferences
- Code block preferences
- 'using' directive preferences (IDE0065)
- Modifier preferences
- Namespace declaration preferences (IDE0160 and IDE0161)
Visual Basic style rules
The style rules in this section are applicable to Visual Basic language only.
See also
Feedback
Submit and view feedback for