You don't have to escape arguments, for example, below shouldn't crash on any 
version of .NET .

We you perhaps instead passing user input as the format string instead? That 
you will have to escape.

From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of Greg Keogh
Sent: Wednesday, February 02, 2011 9:42 PM
To: 'ozDotNet'
Subject: string.Format and curly braces

Back to coding ... I diagnosed an app crash today caused by an argument to 
string.Format having curly braces inside it. I was doing something like 
string.Format("Report title: {0}", title) where title was the string 
"{Intention}" and I'm told this is a perfectly acceptable title.

We all know that you have to escape braces by doubling them in the arguments, 
but at what point in an app do you guard against this crash? Today it was way 
up in the UI, several months ago I had the same crash way down in a server 
logging method. The moderate sized app I'm working on today has 366 
string.Format calls scattered all through it at different levels, some in a 
Silverlight app and some on the server side. How on earth do you globally guard 
all these calls without making a coding mess? I haven't found an obvious 
elegant solution yet. Has anyone else considered this problem?

Sure I could wrap string.Format calls in an another function or create a string 
extension method that doubles braces in the arguments, but it seems clumsy.

Greg

Reply via email to