C# Tips

Today's C# Tip

C# - Curly Braces in string.Format

I ran into an error when using the following string formatting.

// Wrong
var s = string.Format("{ \"data\": {0} }", data); 

//System.FormatException: Input string was not in a correct format.       

I thought there might an issue in data variable. It turns out that it was because I put single curly braces ({, }) instead of double braces {{ and }}.

		
// Correct
var s = string.Format("{{ \"data\": {0} }}", data);