string.Format and curly brackets throwing “Input string was not in a correct format.”

Posted by Mark Milbourne


I just fell into what seems a newbie C# programming pitfall, so I thought I'd document it for someone else's learning pleasure.

Using string.Format() to create a string (ala printf), I was hit by the exception "Input string was not in a correct format." Here's an example of the problem;

string.Format("{0}/{id}", "controller");

While its may seem obvious that its the second curly braces, it does seem silly that it doesn't figure this out correctly. Here's the solution;

string.Format("{0}/{{id}}", "controller");

Which now does look silly, but it works.


Post to Twitter