| Property | Value |
|---|---|
| 規則識別碼 | IDE0043 |
| Title | 格式字串無效 |
| Category | Style |
| 子類別 | 語言規則(字串操作) |
| 適用語言 | C# 與 Visual Basic |
| 選項 | 沒有 |
Overview
此規則會標記對複合格式化方法的呼叫,例如 String.Format,格式字串中的佔位索引指的是不存在的參數。 例如,如果格式字串包含 {1},但只提供一個參數(可透過 {0} 存取),則此規則會標記 {1} 預留位置。
Note
此規則類似於程式碼品質規則 CA2241,後者同樣能驗證格式字串參數。
Example
// Code with violations.
// '{1}' is out of range — only one argument is provided.
string s = string.Format("Hello, {0}! You have {1} messages.", "Alice");
// Fixed code.
string s = string.Format("Hello, {0}! You have {1} messages.", "Alice", 5);
' Code with violations.
' '{1}' is out of range — only one argument is provided.
Dim s As String = String.Format("Hello, {0}! You have {1} messages.", "Alice")
' Fixed code.
Dim s As String = String.Format("Hello, {0}! You have {1} messages.", "Alice", 5)
隱藏警告
如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。
#pragma warning disable IDE0043
// The code that's violating the rule is on this line.
#pragma warning restore IDE0043
若要停用檔案、資料夾或項目的規則,請在none中將其嚴重性設定為。
[*.{cs,vb}]
dotnet_diagnostic.IDE0043.severity = none
若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
如需詳細資訊,請參閱 如何隱藏程式碼分析警告。