新增括號 (IDE0011)

屬性
規則識別碼 IDE0011
標題 新增大括弧
類別 樣式
子類別 語言規則 (程式碼區塊喜好設定)
適用語言 C#
選項 csharp_prefer_braces

概觀

這個樣式規則是有關使用大括弧 { } 括住程式碼區塊。

選項

使用下列選項可以指定有無偏好使用的大括號;如果有,是否只針對多行程式碼區塊使用。

如需設定選項的詳細資訊,請參閱選項格式

csharp_prefer_braces

屬性 描述
選項名稱 csharp_prefer_braces
選項值 true 偏好使用大括號,即使僅一行程式碼
false 如果允許的話,偏好不使用大括號
when_multiline 偏好在多行使用大括號
預設選項值 true
// csharp_prefer_braces = true
if (test) { this.Display(); }

// csharp_prefer_braces = false
if (test) this.Display();

// csharp_prefer_braces = when_multiline
if (test) this.Display();
else { this.Display(); Console.WriteLine("Multiline"); }

隱藏警告

若您只想隱藏單一違規,請將前置處理指示詞新增至來源檔案以停用規則,然後重新啟用規則。

#pragma warning disable IDE0011
// The code that's violating the rule is on this line.
#pragma warning restore IDE0011

若要停用檔案、資料夾或專案的規則,請在組態檔中將其嚴重性設定為 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0011.severity = none

若要停用所有程式碼樣式規則,請在組態檔中將類別 Style 的嚴重性設定為 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

如需詳細資訊,請參閱如何隱藏程式碼分析警告

另請參閱