Редактиране

Споделяне чрез


Code-style rules

.NET code-style analysis provides rules that aim to maintain consistent style in your codebase. These rules have an "IDE" prefix in the rule ID.

The code-style rules are organized into the following subcategories:

  • Language and unnecessary code rules

    Rules that pertain to the C# or Visual Basic language. For example, you can specify rules that regard the use of var when defining variables, or whether expression-bodied members are preferred. This category also includes rules that find unnecessary code, for example, unreachable code within methods or unused private fields, properties, or methods.

  • Formatting rules

    Rules that pertain to the layout and structure of your code in order to make it easier to read. For example, you can specify a formatting option that defines whether spaces in control blocks are preferred or not.

  • Naming rules

    Rules that pertain to the naming of code elements. For example, you can specify that async method names must have an "Async" suffix.

  • Miscellaneous rules

    Rules that do not belong in other categories.

Index

The following table list all the code-style rules by ID and options, if any.

Rule ID Title Option
IDE0001 Simplify name
IDE0002 Simplify member access
IDE0003 Remove this or Me qualification dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event
IDE0004 Remove unnecessary cast
IDE0005 Remove unnecessary import
IDE0007 Use var instead of explicit type csharp_style_var_for_built_in_types
csharp_style_var_when_type_is_apparent
csharp_style_var_elsewhere
IDE0008 Use explicit type instead of var csharp_style_var_for_built_in_types
csharp_style_var_when_type_is_apparent
csharp_style_var_elsewhere
IDE0009 Add this or Me qualification dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event
IDE0010 Add missing cases to switch statement
IDE0011 Add braces csharp_prefer_braces
IDE0016 Use throw expression csharp_style_throw_expression
IDE0017 Use object initializers dotnet_style_object_initializer
IDE0018 Inline variable declaration csharp_style_inlined_variable_declaration
IDE0019 Use pattern matching to avoid as followed by a null check csharp_style_pattern_matching_over_as_with_null_check
IDE0020 Use pattern matching to avoid is check followed by a cast (with variable) csharp_style_pattern_matching_over_is_with_cast_check
IDE0021 Use expression body for constructors csharp_style_expression_bodied_constructors
IDE0022 Use expression body for methods csharp_style_expression_bodied_methods
IDE0023 Use expression body for conversion operators csharp_style_expression_bodied_operators
IDE0024 Use expression body for operators csharp_style_expression_bodied_operators
IDE0025 Use expression body for properties csharp_style_expression_bodied_properties
IDE0026 Use expression body for indexers csharp_style_expression_bodied_indexers
IDE0027 Use expression body for accessors csharp_style_expression_bodied_accessors
IDE0028 Use collection initializers dotnet_style_collection_initializer
dotnet_style_prefer_collection_expression (C# only)
IDE0029 Null check can be simplified dotnet_style_coalesce_expression
IDE0030 Null check can be simplified dotnet_style_coalesce_expression
IDE0031 Use null propagation dotnet_style_null_propagation
IDE0032 Use auto property dotnet_style_prefer_auto_properties
IDE0033 Use explicitly provided tuple name dotnet_style_explicit_tuple_names
IDE0034 Simplify default expression csharp_prefer_simple_default_expression
IDE0035 Remove unreachable code
IDE0036 Order modifiers csharp_preferred_modifier_order
visual_basic_preferred_modifier_order
IDE0037 Use inferred member name dotnet_style_prefer_inferred_tuple_names
dotnet_style_prefer_inferred_anonymous_type_member_names
IDE0038 Use pattern matching to avoid is check followed by a cast (without variable) csharp_style_pattern_matching_over_is_with_cast_check
IDE0039 Use local function instead of lambda csharp_style_prefer_local_over_anonymous_function
IDE0040 Add accessibility modifiers dotnet_style_require_accessibility_modifiers
IDE0041 Use is null check dotnet_style_prefer_is_null_check_over_reference_equality_method
IDE0042 Deconstruct variable declaration csharp_style_deconstructed_variable_declaration
IDE0044 Add readonly modifier dotnet_style_readonly_field
IDE0045 Use conditional expression for assignment dotnet_style_prefer_conditional_expression_over_assignment
IDE0046 Use conditional expression for return dotnet_style_prefer_conditional_expression_over_return
IDE0047 Remove unnecessary parentheses dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators
dotnet_style_parentheses_in_other_operators
IDE0048 Add parentheses for clarity dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators
dotnet_style_parentheses_in_other_operators
IDE0049 Use language keywords instead of framework type names for type references dotnet_style_predefined_type_for_locals_parameters_members
dotnet_style_predefined_type_for_member_access
IDE0050 Convert anonymous type to tuple
IDE0051 Remove unused private member
IDE0052 Remove unread private member
IDE0053 Use expression body for lambdas csharp_style_expression_bodied_lambdas
IDE0054 Use compound assignment dotnet_style_prefer_compound_assignment
IDE0055 Fix formatting (Too many to list here. See .NET formatting options and C# formatting options.)
IDE0056 Use index operator csharp_style_prefer_index_operator
IDE0057 Use range operator csharp_style_prefer_range_operator
IDE0058 Remove unused expression value csharp_style_unused_value_expression_statement_preference
visual_basic_style_unused_value_expression_statement_preference
IDE0059 Remove unnecessary value assignment csharp_style_unused_value_assignment_preference
visual_basic_style_unused_value_assignment_preference
IDE0060 Remove unused parameter dotnet_code_quality_unused_parameters
IDE0061 Use expression body for local functions csharp_style_expression_bodied_local_functions
IDE0062 Make local function static csharp_prefer_static_local_function
IDE0063 Use simple using statement csharp_prefer_simple_using_statement
IDE0064 Make struct fields writable
IDE0065 using directive placement csharp_using_directive_placement
IDE0066 Use switch expression csharp_style_prefer_switch_expression
IDE0070 Use System.HashCode.Combine
IDE0071 Simplify interpolation dotnet_style_prefer_simplified_interpolation
IDE0072 Add missing cases to switch expression
IDE0073 Use file header file_header_template
IDE0074 Use coalesce compound assignment dotnet_style_prefer_compound_assignment
IDE0075 Simplify conditional expression dotnet_style_prefer_simplified_boolean_expressions
IDE0076 Remove invalid global SuppressMessageAttribute
IDE0077 Avoid legacy format target in global SuppressMessageAttribute
IDE0078 Use pattern matching csharp_style_prefer_pattern_matching
IDE0079 Remove unnecessary suppression dotnet_remove_unnecessary_suppression_exclusions
IDE0080 Remove unnecessary suppression operator
IDE0081 Remove ByVal
IDE0082 Convert typeof to nameof
IDE0083 Use pattern matching (not operator) csharp_style_prefer_not_pattern
IDE0084 Use pattern matching (IsNot operator) visual_basic_style_prefer_isnot_expression
IDE0090 Simplify new expression csharp_style_implicit_object_creation_when_type_is_apparent
IDE0100 Remove unnecessary equality operator
IDE0110 Remove unnecessary discard
IDE0120 Simplify LINQ expression
IDE0130 Namespace does not match folder structure dotnet_style_namespace_match_folder
IDE0140 Simplify object creation visual_basic_style_prefer_simplified_object_creation
IDE0150 Prefer null check over type check csharp_style_prefer_null_check_over_type_check
IDE0160 Use block-scoped namespace csharp_style_namespace_declarations
IDE0161 Use file-scoped namespace csharp_style_namespace_declarations
IDE0170 Simplify property pattern csharp_style_prefer_extended_property_pattern
IDE0180 Use tuple to swap values csharp_style_prefer_tuple_swap
IDE0200 Remove unnecessary lambda expression csharp_style_prefer_method_group_conversion
IDE0210 Convert to top-level statements csharp_style_prefer_top_level_statements
IDE0211 Convert to 'Program.Main' style program csharp_style_prefer_top_level_statements
IDE0220 Add explicit cast in foreach loop dotnet_style_prefer_foreach_explicit_cast_in_source
IDE0230 Use UTF-8 string literal csharp_style_prefer_utf8_string_literals
IDE0240 Nullable directive is redundant
IDE0241 Nullable directive is unnecessary
IDE0250 Struct can be made 'readonly' csharp_style_prefer_readonly_struct
IDE0251 Member can be made 'readonly' csharp_style_prefer_readonly_struct_member
IDE0260 Use pattern matching csharp_style_pattern_matching_over_as_with_null_check
IDE0270 Null check can be simplified dotnet_style_coalesce_expression
IDE0280 Use nameof
IDE0290 Use primary constructor csharp_style_prefer_primary_constructors
IDE0300 Use collection expression for array dotnet_style_prefer_collection_expression
IDE0301 Use collection expression for empty dotnet_style_prefer_collection_expression
IDE0302 Use collection expression for stackalloc dotnet_style_prefer_collection_expression
IDE0303 Use collection expression for Create() dotnet_style_prefer_collection_expression
IDE0304 Use collection expression for builder dotnet_style_prefer_collection_expression
IDE0305 Use collection expression for fluent dotnet_style_prefer_collection_expression
IDE1005 Use conditional delegate call csharp_style_conditional_delegate_call
IDE1006 Naming styles

Note

There are some rules that only surface in the Visual Studio IDE, and these regard JSON and regular expression pattern strings. For more information, see JSON001, JSON002, and RE0001.

Legend

The following table shows the type of information that's provided for each rule in the reference documentation.

Item Description
Rule ID The unique identifier for the rule. Used for configuring rule severity and suppressing warnings in the code file.
Title The title for the rule.
Category The category for the rule.
Subcategory The subcategory for the rule, such as Language rules, Formatting rules, or Naming rules.
Applicable languages Applicable .NET languages (C# or Visual Basic), along with the minimum language version, if applicable.
Introduced version Version of the .NET SDK or Visual Studio when the rule was first introduced.
Options Any available options for the rule.

See also