Is CultureInfo.CurrentCulture really necessary in String.Format()?

abatishchev picture abatishchev · Jan 24, 2009 · Viewed 26.4k times · Source

How do you think is really necessary to provide IFormatProvider in method String.Format(string, object) ?

Is it better to write full variant

String.Format(CultureInfo.CurrentCulture, "String is {0}", str);

or just

String.Format("String is {0}", str);

?

Answer

Joe picture Joe · Jan 24, 2009

In general, you will want to use InvariantCulture if the string you are generating is to be persisted in a way that is independent of the current user's culture (e.g. in the registry, or in a file).

You will want to use CurrentCulture for strings that are to be presented in the UI to the current user (forms, reports).

Subtle bugs can arise if you use CurrentCulture where you should be using InvariantCulture: bugs that only come to light when you have multiple users with different cultures accessing the same registry entry or file, or if a user changes his default culture.

Explicitly specifying CurrentCulture (the default if the IFormatProvider argument is omitted), is essentially documentation that demonstrates that you have considered the above and that the string being generated should use the current user's culture. That's why FxCop recommends that you should specify the IFormatProvider argument.