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) orfalse
(do not prefer this style). Other rules accept values such aswhen_on_single_line
ornever
.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 to9.0
or higher, or topreview
. - 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.
- Set the <AnalysisLevel> or
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- Code-block preferences
- Expression-bodied members
- Expression-level preferences
- Field preferences
- Language keyword vs. framework types preferences
- Modifier preferences
- New-line preferences
- Null-checking preferences
- Parameter preferences
- Parentheses preferences
- Pattern-matching preferences
- Suppression preferences
This.
andme.
preferencesvar
preferences
using
directive preferences
.NET style rules (C# and Visual Basic):
C# style rules:
Code-block preferences
C# style rules:
- Add braces (IDE0011)
- Use simple 'using' statement (IDE0063)
- Namespace declaration preferences (IDE0160, IDE0161)
- Remove unnecessary lambda expression (IDE0200)
- Convert to top-level statements (IDE0210)
- Convert to 'Program.Main' style program (IDE0211)
- Use primary constructor (IDE0290)
Expression-bodied members
C# style rules:
- Use expression body for constructors (IDE0021)
- Use expression body for methods (IDE0022)
- Use expression body for operators (IDE0023, 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)
Expression-level preferences
.NET style rules (C# and Visual Basic):
- Simplify name (IDE0001)
- Simplify member access (IDE0002)
- Remove unnecessary cast (IDE0004)
- Add missing cases to switch statement (IDE0010)
- Use object initializers (IDE0017)
- Use collection initializers (IDE0028)
- Null check can be simplified (IDE0029, IDE0030, IDE0270)
- Use null propagation (IDE0031)
- Use auto-implemented property (IDE0032)
- Use explicitly provided tuple name (IDE0033)
- Remove unreachable code (IDE0035)
- Use inferred member names (IDE0037)
- Use 'is null' check (IDE0041)
- Use conditional expression for assignment (IDE0045)
- Use conditional expression for return (IDE0046)
- Convert anonymous type to tuple (IDE0050)
- Remove unused private member (IDE0051)
- Remove unread private member (IDE0052)
- Use compound assignment (IDE0054, IDE0074)
- Remove unnecessary expression value (IDE0058)
- Remove unnecessary value assignment (IDE0059)
- Use 'System.HashCode.Combine' (IDE0070)
- Simplify interpolation (IDE0071)
- Simplify conditional expression (IDE0075)
- Convert
typeof
tonameof
(IDE0082) - Remove unnecessary equality operator (IDE0100)
- Simplify LINQ expression (IDE0120)
- Namespace does not match folder structure (IDE0130)
C# style rules:
- Use throw expression (IDE0016)
- 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)
- Add missing cases to switch expression (IDE0072)
- Remove unnecessary suppression operator (IDE0080)
- Simplify
new
expression (IDE0090) - Remove unnecessary discard (IDE0110)
- Prefer 'null' check over type check (IDE0150)
- Use tuple to swap values (IDE0180)
- Add explicit cast in foreach loop (IDE0220)
- Use UTF-8 string literal (IDE0230)
- Nullable directive is redundant (IDE0240)
- Nullable directive is unnecessary (IDE0241)
- Use collection expression for array (IDE0300)
- Use collection expression for empty (IDE0301)
- Use collection expression for stack alloc (IDE0302)
- Use collection expression for
Create()
(IDE0303) - Use collection expression for builder (IDE0304
- Use collection expression for fluent (IDE0305)
Visual Basic style rules:
- Remove
ByVal
(IDE0081) - Use pattern matching (
IsNot
operator) (IDE0084) - Simplify object creation (IDE0140)
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:
- Make local function static (IDE0062)
- Make struct fields writable (IDE0064)
- Struct can be made 'readonly' (IDE0250)
- Member can be made 'readonly' (IDE0251)
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:
- Use pattern matching to avoid 'as' followed by 'null' check (IDE0019)
- Use pattern matching to avoid 'is' check followed by a cast (IDE0020, IDE0038)
- Use switch expression (IDE0066)
- Use pattern matching (IDE0078 and IDE0260)
- Use pattern matching (
not
operator) (IDE0083) - Simplify property pattern (IDE0170)
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
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for