The Secret of Curly Braces in C#: How to Properly Escape in String.Format

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why are curly braces in C# a headache?



Have you ever written code in C#, wanted to output text like {Name}: {Value}, but instead got a FormatException? If so — you are not alone. The question “How to escape curly brace in String.Format” on Stack Overflow has gathered tens of thousands of views. And this is no coincidence: curly braces {} in C# have a special meaning — they denote a placeholder for variable substitution.



But what if you need to output the braces themselves as regular characters? For example, for JSON, templates, code generation, or just for nicely formatting a report? Today we will break down this topic from start to finish: from simple escaping to advanced tricks with StringBuilder and string interpolation.



How to escape curly braces: the basic rule



The answer is simple and elegant: double the brace. If you want to output a single opening brace {, write {{. For a closing brace } — write }}. C# will understand that this is not a placeholder, but a regular character.



Here is the simplest example:



string result = string.Format("Value in curly braces: {{0}}", 42);Console.WriteLine(result);// Output: Value in curly braces: {0}


Note: {{0}} is not a placeholder, but an escaped pair of braces with the digit 0 inside. If we had written {0}, we would have gotten the variable value (the number 42).



Typical beginner mistake



Beginners often write like this:



// ERROR!string bad = string.Format("Hello, {name}!", name);// FormatException: Input string was not in a correct format.


Why? Because C# expects a numeric index inside the braces ({0}, {1}), not a variable name. For names, you need to use string interpolation (see below) or explicitly specify indices.



Practical examples: from simple to complex



Let's break down several real-world scenarios where escaping braces is necessary.



1. Outputting JSON or XML for debugging purposes



Imagine you are writing a logger and want to output a JSON string with a substituted value:



int userId = 101;string json = string.Format("{{\\"userId\\": {0}, \\"name\\": \\"Alice\\"}}", userId);Console.WriteLine(json);// Output: {"userId": 101, "name": "Alice"}


Here {{ and }} create the outer curly braces of the JSON, and {0} substitutes the number.



2. Generating code templates



Suppose you are writing a code generator in C#. You need to create a string with a template for another language (e.g., Python or JavaScript):



string template = string.Format("def {0}():    return {{0}}", "calculate");Console.WriteLine(template);// Output:// def calculate()://     return {0}


Note: {{0}} in the output string is no longer a C# placeholder, but part of the generated code (e.g., for Python).



3. Formatting with multiple placeholders and braces



When you need to mix substitutions and escaping, the key is not to get confused with the indices:



string name = "Bob";int age = 30;string message = string.Format("User {{ {0} }} (age: {1})", name, age);Console.WriteLine(message);// Output: User { Bob } (age: 30)


Here {{ gives {, }} gives }, and {0} and {1} substitute the values.



Alternatives to String.Format: string interpolation and StringBuilder



Although string.Format is a classic, modern C# has more convenient ways. Let's compare.



String interpolation (C# 6+)



String interpolation is the magic of $ before the string. It reads better, but escaping braces remains

Blogs

Book Recommendations