C#idiomatic C#
ide0043fundamentals/code-analysis/style-rules

Code analysis

Invalid format string (IDE0043)

Learn about code analysis rule IDE0043: Invalid format string

IDE0043 Style / Language rules (string operations)
Applicable languages C# and Visual BasicOptions None

Overview

This rule flags calls to composite formatting methods, such as System.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.

Example

csharp
// 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);
vb
' 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.

csharp
#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.

ini
[*.{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.

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

For more information, see How to suppress code analysis warnings.

See also