ReSharper 2024.1 Help

Code Inspection: Specify string culture explicitly

Category

Common Practices and Code Improvements

ID

SpecifyACultureInStringConversionExplicitly

EditorConfig

resharper_specify_a_culture_in_string_conversion_explicitly_highlighting

Default severity

Warning

Language

C#

Requires SWA

No

Ad-hoc conversion of data structures to text is largely dependent on the current culture, and may lead to unintended results when the code is executed on a machine whose locale differs from that of the original developer. To prevent ambiguities, ReSharper warns you of any instances in code where such a problem may occur.

For example, take the following code

void Test(float foo) { Console.WriteLine(foo.ToString()); ; }

While one might think that a float is culture-invariant, this is in fact not the case: for example, the decimal separator can be different depending on culture. As a result, it often makes sense to specify either a specific culture (for example, the Thread.CurrentThread.CurrentCulture) or an invariant culture CultureInfo.InvariantCulture for string conversions.

Last modified: 15 April 2024