Take the 2-minute tour ×

It's tedious to write

string msg = "Result is " + r.ToString(CultureInfo.InvariantCulture) + " with "
    + p.toString(CultureInfo.InvariantCulture) + "% probability.";

I would prefer

string msg = "Result is " + r + " with " + p + "% probability.";

How can I set InvariantCulture as the default throughout my project?

My goal is to produce programmer readable output.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You need to set the current thread and ui thread cultures to it:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
share|improve this answer
add comment

You example shows the formatting of display text, so you should trust the plain version of ToString() to format your objects as appropriate to the current culture. Therefore don't specify a culture at all. Don't worry, it's how it's meant to be!

InvariantCulture should be reserved for cases where you construct strings that are meant to control logic, not display strings. See for example this MSDN page which confirms this.

If for some obscure reason you really don't want any variation in your display strings then you can specify a culture, any culture (e.g. Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US");), but not InvariantCulture.

share|improve this answer
add comment

Your Answer


