How to use curly brackets inside of String.Format
String.Format provides inserting variables inside of the string, indicated with curly brackets and index, for example
String s = string.Format"Hello {0}", name)
But if you need to use curly brackets inside of string format you will get exception, because compiler does not know if you trying to insert curly brackets or replacing string. So you can get exception
String s = string.Format("{name} = {0}", name); // exception
But there is a simple solution to avoid exception, you need to use double curly brackets.
String s = string.Format("{{name}} = {0}", name); // ok
Tags: c#
Trackback from your site.
