Cloud Training (Online)

C# Tips and Tricks #7 – Escaping ‘{‘ in C# String.Format

When you are using the string.format , you would have noticed that it might be challenging to have the flower bracket “{” if you want it be to displayed as a part of the string.

For example , you want to show the full method syntax something like this using the string.format.

string method = String.Format(“{0} {1} SetFirstName(string name) {  }”, “public”, “void”);

When you try to run this to display the full syntax of the method , you will encounter the run time error.

SNAGHTML15bace19In order to fix this and show the { and } within the string.Format , you might want to escape the character and display it. The simple solution will be to use double braces as shown below.

using System;
namespace ConsoleApp1
     class Program
         static void Main(string[] args)

            string method = String.Format("{0} {1} SetFirstName(string name) {{  }}", "public", "void");
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.