Code Inspection: Specify string culture explicitly
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, JetBrains Rider 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: 21 March 2024