views:

98

answers:

1

How do I display a literal curly brace character when using the String.Format method?

Example:

sb.AppendLine(String.Format("public {0} {1} { get; private set; }", 
prop.Type, prop.Name));

I would like the output to look like this:

public Int32 MyProperty { get; private set; }
+16  A: 

Use double braces {{ or }} so your code becomes:

sb.AppendLine(String.Format("public {0} {1} {{ get; private set; }}", prop.Type, prop.Name));
Richard Cook
Straight from the documentation: <quote>To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".</quote> http://msdn.microsoft.com/en-us/library/b1csw23d.aspx
Ben Voigt