程式碼樣式規則

.NET 程式代碼樣式分析提供規則,旨在維護程式碼基底中的一致 樣式 。 這些規則在規則識別碼中有 "IDE" 前置詞。

程式碼樣式規則會組織成下列子類別:

  • 語言和不必要的程式代碼規則

    有關 C# 或 Visual Basic 語言的規則。 例如,您可以在定義變數或偏好的運算式主體成員時,指定要使用 var 的規則。 此類別也包含尋找不必要的程式碼的規則,例如,方法內無法連線的程式代碼,或未使用的私用字段、屬性或方法。

  • 格式化規則

    有關程式碼配置和結構的規則,以使其更容易閱讀。 例如,您可以指定格式化選項,以定義控制項區塊中的空間是否要設為偏好空間。

  • 命名規則

    與程式碼項目命名相關的規則。 例如,您可以指定 async 方法名稱必須具有 "Async" 尾碼。

  • 其他規則

    不屬於其他類別的規則。

索引

下表列出識別碼和選項的所有程式碼樣式規則,如果有的話。

規則識別碼 標題 選項
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 移除非必要的匯入
IDE0007 使用 var,而非明確類型 csharp_style_var_for_built_in_types
csharp_style_var_when_type_is_apparent
csharp_style_var_elsewhere
IDE0008 使用明確類型,而非 var csharp_style_var_for_built_in_types
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_style_object_initializer
IDE0018 內嵌變數宣告 csharp_style_inlined_variable_declaration
IDE0019 使用模式比對以避免 as 後面接著 null 檢查 csharp_style_pattern_matching_over_as_with_null_check
IDE0020 使用模式比對以避免 is 檢查後面接著轉換 (含變數) csharp_style_pattern_matching_over_is_with_cast_check
IDE0021 使用建構函式的運算式主體 csharp_style_expression_bodied_constructors
IDE0022 使用方法的運算式主體 csharp_style_expression_bodied_methods
IDE0023 使用轉換運算子的運算式主體 csharp_style_expression_bodied_operators
IDE0024 使用運算子的運算式主體 csharp_style_expression_bodied_operators
IDE0025 使用屬性的運算式主體 csharp_style_expression_bodied_properties
IDE0026 使用索引子的運算式主體 csharp_style_expression_bodied_indexers
IDE0027 使用存取子的運算式主體 csharp_style_expression_bodied_accessors
IDE0028 使用集合初始設定式 dotnet_style_collection_initializer
dotnet_style_prefer_collection_expression (僅限 C#)
IDE0029 可以簡化 Null 檢查 dotnet_style_coalesce_expression
IDE0030 可以簡化 Null 檢查 dotnet_style_coalesce_expression
IDE0031 使用 null 傳播 dotnet_style_null_propagation
IDE0032 使用 Auto 屬性 dotnet_style_prefer_auto_properties
IDE0033 使用明確提供的元組名稱 dotnet_style_explicit_tuple_names
IDE0034 簡化 default 運算式 csharp_prefer_simple_default_expression
IDE0035 移除執行不到的程式碼
IDE0036 為修飾元排序 csharp_preferred_modifier_order
visual_basic_preferred_modifier_order
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 新增唯讀修飾元 dotnet_style_readonly_field
IDE0045 使用條件運算式進行指派 dotnet_style_prefer_conditional_expression_over_assignment
IDE0046 使用條件運算式傳回 dotnet_style_prefer_conditional_expression_over_return
IDE0047 移除非必要的括號 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 新增括號以明確表示 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 在型別參考中使用語言關鍵字替代架構型別名稱 dotnet_style_predefined_type_for_locals_parameters_members
dotnet_style_predefined_type_for_member_access
IDE0050 將匿名型別轉換為元組
IDE0051 移除未使用的私人成員
IDE0052 移除未讀取的私人成員
IDE0053 使用 Lambda 的運算式主體 csharp_style_expression_bodied_lambdas
IDE0054 使用複合指派 dotnet_style_prefer_compound_assignment
IDE0055 修正格式化 (太多人無法在這裡列出。請參閱 .NET 格式設定選項C# 格式設定選項。)
IDE0056 使用索引運算子 csharp_style_prefer_index_operator
IDE0057 使用範圍運算子 csharp_style_prefer_range_operator
IDE0058 移除未使用的運算式值 csharp_style_unused_value_expression_statement_preference
visual_basic_style_unused_value_expression_statement_preference
IDE0059 移除非必要的值指派 csharp_style_unused_value_assignment_preference
visual_basic_style_unused_value_assignment_preference
IDE0060 移除未使用的參數 dotnet_code_quality_unused_parameters
IDE0061 使用區域函式的運算式主體 csharp_style_expression_bodied_local_functions
IDE0062 將區域函式設為靜態 csharp_prefer_static_local_function
IDE0063 使用簡易的 using 陳述式 csharp_prefer_simple_using_statement
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 使用檔案標頭 file_header_template
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_style_prefer_isnot_expression
IDE0090 簡化 new 運算式 csharp_style_implicit_object_creation_when_type_is_apparent
IDE0100 移除非必要的相等運算子
IDE0110 移除不必要的捨棄項目
IDE0120 簡化 LINQ 運算式
IDE0130 命名空間與資料夾結構不相符 dotnet_style_namespace_match_folder
IDE0140 簡化物件建立 visual_basic_style_prefer_simplified_object_creation
IDE0150 建議使用 null 檢查而非型別檢查 csharp_style_prefer_null_check_over_type_check
IDE0160 使用已設定區塊範圍的命名空間 csharp_style_namespace_declarations
IDE0161 使用已設定檔案範圍的命名空間 csharp_style_namespace_declarations
IDE0170 簡化屬性模式 csharp_style_prefer_extended_property_pattern
IDE0180 使用元組交換值 csharp_style_prefer_tuple_swap
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 可為 Null 的指示詞是多餘的
IDE0241 不需要 Null 指示詞
IDE0250 結構可以「只讀」 csharp_style_prefer_readonly_struct
IDE0251 成員可以設為「只讀」 csharp_style_prefer_readonly_struct_member
IDE0260 使用模式比對 csharp_style_pattern_matching_over_as_with_null_check
IDE0270 可以簡化 Null 檢查 dotnet_style_coalesce_expression
IDE0280 使用 nameof
IDE0290 使用主要建構函式 csharp_style_prefer_primary_constructors
IDE0300 使用陣列的集合表達式 dotnet_style_prefer_collection_expression
IDE0301 針對空白使用集合表達式 dotnet_style_prefer_collection_expression
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
IDE1005 使用條件式委派呼叫 csharp_style_conditional_delegate_call
IDE1006 命名樣式

注意

有些規則只會呈現在Visual Studio IDE 中,而這些規則則涉及 JSON 和正則表示式模式字串。 如需詳細資訊,請參閱 JSON001JSON002RE0001

圖例

下表顯示針對參考文件中每個規則所提供的資訊類型。

項目 描述
規則識別碼 規則的唯一識別碼。 用於設定規則嚴重性和隱藏程式碼檔案中的警告。
標題 規則的標題。
類別 規則的類別。
子類別 規則的子類別,例如語言規則、格式化規則或命名規則。
適用語言 適用的 .NET 語言 (C# 或 Visual Basic),以及適用的最低語言版本。如果適用的話。
引進的版本 第一次引進規則時的 .NET SDK 或 Visual Studio 版本。
選項 規則的任何可用選項。

另請參閱