How to escape braces (curly brackets) in a format string in .NET
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In .NET formatting APIs, braces are not ordinary characters. They mark placeholders such as {0} or interpolation expressions such as {value}. If you want a literal brace in the output, you must escape it correctly for the formatting mechanism you are using.
Use Double Braces in Composite Formatting
For string.Format, Console.WriteLine, and other composite-format APIs, a literal left brace is written as {{ and a literal right brace as }}.
Output:
The rule is simple: double braces mean "treat this brace as text instead of as a format item".
The Same Rule Applies to String Interpolation
Interpolated strings use braces for expressions, so literal braces must still be doubled.
That prints the same output as the string.Format example. The formatting feature changed, but the escape rule did not.
Distinguish Placeholder Braces from Literal Braces
This is a common source of confusion:
- '
{0}means a placeholder in composite formatting' - '
{value}means an expression in interpolation' - '
{{means a literal{' - '
}}means a literal}'
Here is a concrete comparison:
Reading those forms side by side makes the parser rules easier to remember.
Nested-Looking Cases Are Usually Just Mixed Literal and Expression Parts
Sometimes you want output that contains both literal braces and a formatted value inside them, such as {42}. The cleanest way is to double the outer literal braces and keep the real expression in the middle.
Output:
There are three opening braces in a row in the source:
- the first two
{{become one literal{ - the third
{value}starts the interpolation expression
The same pattern applies on the closing side.
Prefer Simpler Strings When Readability Starts to Suffer
Brace-heavy format strings can become hard to read, especially when generating JSON-like or template-like output. In those cases, consider breaking the text into pieces instead of forcing everything into one expression.
This is not always prettier, but it can be easier to maintain than a single string with many repeated braces.
Why Errors Happen
When the parser sees a single unmatched brace in a format string, it assumes you intended a placeholder and throws a format-related exception or refuses to compile an interpolated string. The issue is not the brace character itself. The issue is that braces are part of the formatting syntax.
That means the fix is not adding a backslash. In .NET formatting strings, the normal fix is doubling the brace.
Common Pitfalls
- Trying to escape braces with backslashes instead of doubling them.
- Forgetting that interpolation uses braces too, so the same literal-brace rule still applies.
- Miscounting braces in strings such as
$"{{{value}}}". - Writing large JSON or template payloads inline and making the format string unreadable.
- Mixing composite formatting and interpolation without noticing which parser is active.
Summary
- In .NET format strings, literal braces are written as
{{and}}. - The rule applies to both composite formatting and string interpolation.
- To output something like
{42}, combine doubled outer braces with a normal expression:$"{{{value}}}". - Braces are syntax in formatting strings, so unmatched single braces cause parsing problems.
- If the escaping becomes hard to follow, simplify the string construction instead of stacking more braces.

