共用方式為


新增大括弧 (IDE0011)

財產 價值
規則標識碼 IDE0011
標題 新增大括弧
類別 風格
子類別 語言規則 (程式代碼區塊喜好設定)
適用的語言 C#
選項 csharp_prefer_braces

概述

此樣式規則涉及使用大括號 { } 括住程式代碼區塊。

選項

使用下列選擇選項可指定是否偏好使用花括弧,如果偏好,是否只針對多行程式代碼區塊。

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

csharp_prefer_braces

財產 價值 描述
選項名稱 C#_偏好使用大括號
選項值 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

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

另請參閱