共用方式為


程式碼樣式規則

.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 移除不必要的類型轉換
IDE0005 移除非必要的匯入
IDE0007 使用 var,而非明確類型 csharp_style_var_for_built_in_types(C# 樣式內建類型的變數命名)
當類型明顯時使用var的C#風格
csharp_style_var_elsewhere
IDE0008 使用明確類型,而非 var csharp_style_var_for_built_in_types(C# 樣式內建類型的變數命名)
當類型明顯時使用var的C#風格
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(C# 樣式丟擲表達式)
IDE0017 使用物件初始設定式 dotnet_style_object_initializer
IDE0018 內嵌變數宣告 C# 風格內嵌變數宣告
IDE0019 使用模式比對以避免 as 後面接著 null 檢查 csharp_樣式_以模式匹配取代_作為 null 檢查
IDE0020 使用模式比對以避免 is 檢查後面接著轉換 (含變數) csharp_style_pattern_matching_over_is_with_cast_check(C# 樣式的模式匹配優於“is”檢查和類型轉換)
IDE0021 使用運算式主體來定義建構函式 csharp_style_expression_bodied_constructors(C# 表達式樣式構造器)
IDE0022 使用以表達式為主體的方法 csharp_style_表達式-bodied_methods
IDE0023 使用轉換運算子的運算式主體語法 csharp_style_表達式主體運算符
IDE0024 為運算子使用運算式化主體 csharp_style_表達式主體運算符
IDE0025 屬性應使用運算式主體 csharp_style_expression_bodied_properties
IDE0026 使用索引子的運算式主體 csharp_樣式_運算式為主體的索引
IDE0027 使用存取子的運算式主體 csharp_style_expression_bodied_accessors
IDE0028 使用集合初始設定式 dotnet_style_collection_initializer
dotnet_style_prefer_collection_expression (此為 .NET 的程式碼風格設定名稱)
IDE0029 可以簡化空值檢查 dotnet_style_coalesce_expression
IDE0030 可以簡化空值檢查 dotnet_style_coalesce_expression
IDE0031 使用空值傳遞 dotnet_style_null_propagation
IDE0032 使用自動屬性 dotnet_style_prefer_auto_properties (偏好使用自動屬性)
IDE0033 請使用明確提供的元組名稱 dotnet_style_explicit_tuple_names(明確的元組名稱樣式)
IDE0034 簡化 default 運算式 csharp_prefer_simple_default_expression
IDE0035 移除執行不到的程式碼
IDE0036 訂單修飾項 csharp_偏好修飾符順序
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(C# 樣式的模式匹配優於“is”檢查和類型轉換)
IDE0039 使用區域函式而非 Lambda csharp_style_prefer_local_over_anonymous_function (將區域函數優先於匿名函數的 C# 程式風格)
IDE0040 新增可及性修飾詞 dotnet_style_require_accessibility_modifiers (要求可見性修飾符的程式碼風格)
IDE0041 使用「is null」檢查 dotnet_style_prefer_is_null_check_over_reference_equality_method
IDE0042 解構變數宣告 C# 解構變數宣告風格
IDE0044 新增唯讀修飾元 dotnet_style_readonly_field
IDE0045 使用條件運算式進行指派 dotnet_style_prefer_conditional_expression_over_assignment
IDE0046 使用條件運算式來傳回 dotnet_style_prefer_conditional_expression_over_return
IDE0047 移除非必要的括號 dotnet_運算式中雙目運算子的括號風格
dotnet_style_parentheses_in_relational_binary_operators
dotnet_style_parentheses_in_other_binary_operators(設定其他二元運算子的圓括號風格)
dotnet_style_parentheses_in_other_operators
IDE0048 新增括號以明確表示 dotnet_運算式中雙目運算子的括號風格
dotnet_style_parentheses_in_relational_binary_operators
dotnet_在其他二元運算子中的括號樣式
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 的運算式主體 C# 程式風格的表達式體羣集
IDE0054 使用複合指派 dotnet_style_prefer_compound_assignment
IDE0055 修正格式化 (太多項目無法在這裡列出。請參閱 .NET 格式設定選項C# 格式設定選項。)
IDE0056 使用索引運算子 csharp_style_prefer_index_operator
IDE0057 使用範圍運算子 C# 風格偏好範圍運算符(csharp_style_prefer_range_operator)
IDE0058 移除未使用的運算式值 C# 未使用的值表達式語句首選樣式
visual_basic_style_unused_value_expression_statement_preference
IDE0059 移除非必要的值指派 csharp_style_unused_value_assignment_preference(C#程式碼風格設定,用於未使用之值的指派偏好)
visual_basic_style_unused_value_assignment_preference
IDE0060 移除未使用的參數 dotnet_code_quality_unused_parameters (使用未使用之參數的程式碼品質)
IDE0061 使用區域函式的表示式主體 csharp_style_expression_bodied_local_functions
IDE0062 建立區域函式 static csharp_偏好靜態區域函式
IDE0063 使用簡易的 using 陳述式 C# 偏好簡單的 using 語句
IDE0064 讓結構欄位可寫入
IDE0065 using 指令位置 csharp_using_directive_placement
IDE0066 使用 switch 運算式 csharp_style_prefer_switch_expression(C#風格偏好使用switch表達式)
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(C#風格偏好模式匹配)
IDE0079 移除不必要的抑制 dotnet_remove_unnecessary_suppression_exclusions
IDE0080 移除非必要的抑制運算子
IDE0081 移除 ByVal
IDE0082 typeof 轉換為 nameof
IDE0083 使用模式比對 (not 運算子) csharp_style_prefer_not_pattern
IDE0084 使用模式比對 (IsNot 運算子) 視覺化基本風格偏好「不是」運算式
IDE0090 簡化 new 運算式 csharp_style_隱式物件創建_當類型明顯時
IDE0100 移除非必要的相等運算子
IDE0110 移除不必要的捨棄項目
IDE0120 簡化 LINQ 運算式
IDE0121 簡化 LINQ 類型檢查和轉換
IDE0130 命名空間與資料夾結構不相符 dotnet_style_namespace_match_folder(名稱空間樣式匹配資料夾)
IDE0140 簡化物件建立 避免繁複物件創造的視覺化基本程式風格
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(偏好使用頂層語句的C#風格)
IDE0220 在 foreach 循環中新增明確轉換 dotnet_style_prefer_foreach_explicit_cast_in_source(控制預設配置選項以偏好在 foreach 中顯式類型轉換的設定)
IDE0230 使用UTF-8字串常值 csharp_style_prefer_utf8_string_literals
IDE0240 可為 Null 的指令是多餘的
IDE0241 未必要使用可空性指示詞
IDE0250 結構體可以被設為「唯讀」 csharp_style_prefer_readonly_struct(偏好唯讀結構)
IDE0251 成員可以設為「只讀」 csharp_style_prefer_readonly_struct_member(偏好唯讀結構成員的 C# 風格)
IDE0260 使用模式比對 csharp_樣式_以模式匹配取代_作為 null 檢查
IDE0270 可以簡化空值檢查 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 使用集合表達式以實現流暢調用 dotnet_style_prefer_collection_expression
IDE0306 使用集合表達式來創建新實例 dotnet_style_prefer_collection_expression
IDE0320 建立匿名函式 static csharp_prefer_static_anonymous_function (C# 偏好靜態匿名函數)
IDE0330 偏好 'System.Threading.Lock' csharp_prefer_system_threading_lock
IDE0340 使用未系結的泛型類型 `csharp_style_prefer_unbound_generic_type_in_nameof`屬性設定為 true 時,在使用 nameof 運算子時偏好使用非約束泛型類型。
IDE0350 使用隱含類型的 Lambda 運算式 偏好隱式型別的 lambda 表達式風格設定 (csharp_style_prefer_implicitly_typed_lambda_expression)
IDE1005 使用條件式委派呼叫 csharp_style_條件委託呼叫
IDE1006 命名樣式
IDE3000 使用 Copilot 實作

注意

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

圖例

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

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

另請參閱