ReSharper 2025.1 Help

Code inspection: Do not use right brace after a format specifier in format strings

This inspection highlights cases where a format specifier inside a composite or interpolated string unintentionally captures a closing brace (}). This can cause runtime errors or incorrect formatting behavior.

For example, in the following code:

string result = string.Format("{0:x}}}", 42);

Here, the second closing brace (}}) is misinterpreted by the formatting logic as part of a nested format item. This leads to runtime errors or unintended formatting results.

String interpolation, such as Console.WriteLine($"Value: {42:x}}}");, is internally converted to a call to string.Format in .NET Framework. So, this issue also occurs when using string interpolation.

You can find more about composite formatting in the official Microsoft documentation.

Starting with .NET 5, this issue is handled by the compiler. Format specifiers are now processed in a way that reduces ambiguity, ensuring that additional closing braces do not cause errors. This eliminates the need for manual adjustments in most cases.

For older projects targeting .NET Framework or .NET versions before 5.0, manually changing the expression to a combination of ToString and string concatenation is the simplest way to resolve this issue while ensuring clarity and compatibility.

In this case, the corrected version would look like this:

string result = 42.ToString("x") + "}";
Last modified: 24 March 2025