| 財產 | 價值 |
|---|---|
| 規則標識碼 | IDE0021 |
| 標題 | 使用建構函式的表達式主體 |
| 類別 | 風格 |
| 子類別 | 語言準則(表達式主體成員) |
| 適用的語言 | C# |
| 選項 | csharp_style_expression_bodied_constructors |
概述
此樣式規則涉及使用 表達式主體 與建構函式區塊主體。
選項
設定與此規則相關聯選項的值,以指定是否偏好使用建構函式的表達式主體或區塊主體;如果偏好使用表達式主體,則需指定是否僅針對單行運算式使用它們。
csharp_style_expression_bodied_constructors
| 財產 | 價值 | 描述 |
|---|---|---|
| 選項名稱 | csharp_style_expression_bodied_constructors | |
| 選項值 | true |
優先使用建構函式的表達式形式 |
when_on_single_line |
當建構函式只有單行時,建議使用表達式主體 | |
false |
偏好使用區塊主體於建構函式中 | |
| 預設選項值 | false |
// csharp_style_expression_bodied_constructors = true
public Customer(int age) => Age = age;
// csharp_style_expression_bodied_constructors = false
public Customer(int age) { Age = age; }
隱藏警告
如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。
#pragma warning disable IDE0021
// The code that's violating the rule is on this line.
#pragma warning restore IDE0021
若要停用檔案、資料夾或項目的規則,請將其嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_diagnostic.IDE0021.severity = none
若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
如需詳細資訊,請參閱 如何在隱藏程式代碼分析警告。