Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
| Property | Value |
|---|---|
| Rule ID | IDE0043 |
| Title | Invalid format string |
| Category | Style |
| Subcategory | Language rules (string operations) |
| Applicable languages | C# and Visual Basic |
| Options | None |
Overview
This rule flags calls to composite formatting methods, such as String.Format, where a placeholder index in the format string refers to an argument that doesn't exist. For example, if the format string contains {1} but only one argument is provided (accessible as {0}), the rule flags the {1} placeholder.
Note
This rule is similar to code quality rule CA2241, which also validates format string arguments.
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)
Suppress a warning
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable IDE0043
// The code that's violating the rule is on this line.
#pragma warning restore IDE0043
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0043.severity = none
To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.