代码样式规则

.NET 代码样式分析提供旨在保持代码库中一致 样式 的规则。 这些规则的规则 ID 具有“IDE”前缀。

代码样式规则组织为以下子类别:

  • 语言和不必要的代码规则

    与 C# 或 Visual Basic 语言相关的规则。 例如,可以指定定义变量时有关使用 var 的规则,或有关是否首选 expression-bodied 成员的规则。 此类别还包括查找不必要的代码的规则,例如,方法中无法访问的代码或未使用的私有字段、属性或方法。

  • 格式设置规则

    与代码的布局和结构有关的规则,其作用是使代码更易于阅读。 例如,可以指定一个格式设置选项,用于定义控件块中的空格是否首选。

  • 命名规则

    与代码元素命名相关的规则。 例如,可以指定 async 方法名必须具有“Async”后缀。

  • 其他规则

    不属于其他类别的规则。

索引

下表按 ID 和 选项列出所有代码样式规则(如果有)。

规则 ID 标题 选项
IDE0001 简化名称
IDE0002 简化成员访问
IDE0003 删除 thisMe 限定 dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event
IDE0004 删除不必要的 cast
IDE0005 删除不必要的 import
IDE0007 var 替代显式类型 csharp_风格_var_内置类型
csharp_style_var_when_type_is_apparent
csharp_style_var_elsewhere
IDE0008 用显式类型代替 var 用于内置类型的C#风格var
csharp_style_var_when_type_is_apparent
csharp_style_var_elsewhere
IDE0009 添加 thisMe 限定 dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event
IDE0010 将缺失的事例添加到 switch 语句
IDE0011 添加大括号 csharp_prefer_braces
IDE0016 使用 throw 表达式 csharp_style_throw_expression
IDE0017 使用对象初始值设定项 dotnet风格的对象初始化器
IDE0018 内联变量声明 csharp_style_嵌入式变量声明
IDE0019 使用模式匹配来避免 as 后跟 null 检查 csharp_style_pattern_matching_over_as_with_null_check
IDE0020 使用模式匹配来避免后跟强制转换的 is 检查(带变量) csharp_style_pattern_matching_over_is_with_cast_check(C# 风格模式匹配优于 is 加类型转换检查)
IDE0021 使用构造函数的表达式主体 csharp_style_expression_bodied_constructors
IDE0022 使用方法的表达式主体 csharp_style_expression_bodied_methods
IDE0023 使用转换运算符的表达式主体 C#_样式_表达式主体运算符
IDE0024 使用运算符的表达式主体 Please retain the term as is: csharp_style_expression_bodied_operators
IDE0025 使用属性的表达式主体 C# 风格表达式体属性
IDE0026 使用索引器的表达式主体 csharp_style_expression_bodied_indexers
IDE0027 为访问器使用表达式主体 csharp_style_expression_bodied_accessors
IDE0028 使用集合初始值设定项 dotnet_style_collection_initializer
dotnet_style_prefer_collection_expression
IDE0029 可以简化 Null 检查 dotnet_style_coalesce_expression
IDE0030 可以简化 Null 检查 dotnet_style_coalesce_expression
IDE0031 使用 Null 传播 dotnet_style_null_propagation
IDE0032 使用自动属性 dotnet_style_prefer_auto_properties (dotnet 样式倾向自动属性)
IDE0033 使用显式提供的元组名称 dotnet_style_explicit_tuple_names
IDE0034 简化 default 表达式 csharp_优先使用简单默认表达式
IDE0035 删除无法访问的代码
IDE0036 对修饰符进行排序 csharp_preferred_modifier_order
Visual Basic 首选修饰符顺序
IDE0037 使用推断的成员名称 dotnet_style_prefer_inferred_tuple_names
dotnet_style_prefer_inferred_anonymous_type_member_names
IDE0038 使用模式匹配来避免后跟强制转换的 is 检查(不带变量) csharp_style_pattern_matching_over_is_with_cast_check
IDE0039 使用本地函数而不是 Lambda csharp_style_prefer_local_over_anonymous_function 设置中,在局部函数上优先于匿名函数的编程风格。
IDE0040 添加可访问性修饰符 dotnet_style_require_accessibility_modifiers
IDE0041 使用 is null 检查 dotnet_style_prefer_is_null_check_over_reference_equality_method
IDE0042 析构变量声明 csharp_style_deconstructed_variable_declaration
IDE0044 添加 readonly 修饰符 dotnet_style_readonly_field
IDE0045 使用 assignment 的条件表达式 dotnet_风格_偏好条件表达优于赋值
IDE0046 使用 return 的条件表达式 dotnet_style_prefer_conditional_expression_over_return
IDE0047 删除不必要的括号 dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_关系二元运算符中的括号风格
dotnet_style_parentheses_in_other_binary_operators(在其他二元运算符中使用括号的风格)
dotnet_style_parentheses_in_other_operators
IDE0048 为清楚起见,请添加括号 dotnet_style_parentheses_in_arithmetic_binary_operators
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators
dotnet_在其他运算符中的括号样式
IDE0049 使用语言关键字,而非类型引用的框架类型名称 dotnet_style_预定义类型_用于_局部变量_参数_成员
dotnet_style_predefined_type_for_member_access
IDE0050 将匿名类型转换为元组
IDE0051 删除未使用的私有成员
IDE0052 删除未读取的私有成员
IDE0053 使用 Lambda 的表达式主体 csharp_样式_表达式体_匿名函数
IDE0054 使用复合分配 dotnet_style_prefer_compound_assignment
IDE0055 修正格式 (太多无法在此处列出。请参阅 .NET 格式设置选项C# 格式设置选项
IDE0056 使用索引运算符 csharp_风格_更倾向_索引_运算符
IDE0057 使用范围运算符 C# 样式优选范围运算符
IDE0058 删除未使用的表达式值 csharp_style_unused_value_expression_statement_preference
视觉基础样式未使用值表达式语句偏好
IDE0059 删除不必要的赋值 csharp_style_unused_value_assignment_preference
Visual Basic 样式未使用值分配首选项
IDE0060 删除未使用的参数 dotnet代码质量_未使用的参数
IDE0061 使用局部函数的表达式主体 csharp_style_expression_bodied_local_functions
IDE0062 生成本地函数 static csharp_prefer_static_local_function
IDE0063 使用简单的 using 语句 csharp_偏向简单使用语句
IDE0064 将结构字段设置为可写
IDE0065 using 指令放置 csharp_using_directive_placement
IDE0066 使用 switch 表达式 csharp_style_prefer_switch_expression
IDE0070 使用 System.HashCode.Combine
IDE0071 简化内插 dotnet_style_prefer_simplified_interpolation
IDE0072 将缺失的事例添加到 switch 表达式
IDE0073 使用文件头 文件头模板
IDE0074 使用联合复合赋值 dotnet_style_prefer_compound_assignment
IDE0075 简化条件表达式 dotnet_style_prefer_simplified_boolean_expressions
IDE0076 删除无效的全局 SuppressMessageAttribute
IDE0077 避免在全局 SuppressMessageAttribute 中使用旧格式目标
IDE0078 使用模式匹配 csharp_style_prefer_pattern_matching
IDE0079 删除不必要的抑制 dotnet_remove_unnecessary_suppression_exclusions
IDE0080 删除不必要的抑制运算符
IDE0081 删除了 ByVal
IDE0082 typeof 转换为 nameof
IDE0083 使用模式匹配(not 运算符) csharp_style_prefer_not_pattern
IDE0084 使用模式匹配(IsNot 运算符) Visual Basic 样式偏好:IsNot 表达式
IDE0090 简化 new 表达式 csharp_类型明显时的隐式对象创建风格
IDE0100 删除不必要的相等运算符
IDE0110 删除不必要的弃元
IDE0120 简化 LINQ 表达式
IDE0121 简化 LINQ 类型检查和强制转换
IDE0130 命名空间与文件夹结构不匹配 dotnet_style_namespace_match_folder
IDE0140 简化对象的创建 Visual Basic 风格偏好简化对象创建
IDE0150 首选 null 检查,而不是类型检查 csharp_风格_更喜欢_null检查而不是类型检查
IDE0160 使用以程序块为作用域的命名空间 csharp_style_namespace_declarations
IDE0161 使用以文件为作用域的命名空间 csharp_style_namespace_declarations
IDE0170 简化属性模式 csharp_style_prefer_extended_property_pattern
IDE0180 使用元组来交换值 csharp 风格首选元组交换
IDE0200 删除不必要的 Lambda 表达式 csharp_style_prefer_method_group_conversion
IDE0210 转换为顶级语句 csharp_style_prefer_top_level_statements
IDE0211 转换为“Program.Main”样式程序 csharp_style_prefer_top_level_statements
IDE0220 在 foreach 循环中添加显式强制转换 dotnet_style_prefer_foreach_explicit_cast_in_source
IDE0230 使用 UTF-8 字符串字面量 csharp_style_prefer_utf8_string_literals
IDE0240 Nullable 指令是冗余的
IDE0241 Nullable 指令是不必要的
IDE0250 可以将结构设为“只读” csharp_style_prefer_readonly_struct
IDE0251 成员可设为“只读” csharp_style_prefer_readonly_struct_member
IDE0260 使用模式匹配 csharp_样式_使用模式匹配代替_as_与空检查
IDE0270 可以简化 Null 检查 dotnet_style_coalesce_expression
IDE0280 使用 nameof
IDE0290 使用主构造函数 csharp_style_prefer_primary_constructors
IDE0300 使用 array 的集合表达式 dotnet_style_prefer_collection_expression
IDE0301 使用 empty 的集合表达式 dotnet_style_prefer_collection_expression(.NET框架中的代码风格设置)
IDE0302 使用 stackalloc 的集合表达式 dotnet_style_prefer_collection_expression
IDE0303 使用集合表达式 Create() dotnet_style_prefer_collection_expression
IDE0304 使用生成器的集合表达式 dotnet_style_prefer_collection_expression
IDE0305 使用 fluent 的集合表达式 dotnet_style_prefer_collection_expression
IDE0306 将集合表达式用于新的对象 dotnet_style_prefer_collection_expression
IDE0320 生成匿名函数 static csharp_prefer_static_anonymous_function
IDE0330 首选“System.Threading.Lock” csharp_prefer_system_threading_lock
IDE0340 使用未绑定泛型类型 csharp_style_prefer_unbound_generic_type_in_nameof
IDE0350 使用隐式类型 Lambda 表达式 csharp_风格_偏好_隐式类型化_lambda_表达式
IDE1005 使用条件委托调用 csharp 风格的条件委托调用
IDE1006 命名样式
IDE3000 使用 Copilot 实现

注意

某些规则仅显示在 Visual Studio IDE 中,这些规则涉及 JSON 和正则表达式模式字符串。 有关详细信息,请参阅 JSON001JSON002RE0001

图例

下表显示了参考文档中每个规则提供的信息类型。

说明
规则 ID 规则的唯一标识符。 用于在代码文件中配置规则严重性并禁止显示警告。
标题 规则的标题。
类别 规则的类别。
子类别 规则的子类别,例如语言规则、格式规则或命名规则。
适用的语言 适用的 .NET(C# 或 Visual Basic),以及最低语言版本(如果适用)。
引入的版本 首次引入规则时 .NET SDK 或 Visual Studio 的版本。
选项 规则的任何可用选项。

另请参阅