Sachin Kainth
Sachin Kainth

Reputation: 46760

Specify a culture in string conversion explicitly

I have a loop in which I call ToString() on the int variable i.

for (int i = 1; i <= n; i++)
{
    @Html.ActionLink(@i.ToString(), "Index")
}

Resharper tells me that I should:

Specify a culture in string conversion explicitly.

Why is this?

How many ways are there to convert an int into a string?

I would have thought only 1.

Upvotes: 8

Views: 7250

Answers (2)

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

There are different methods of grouping symbols, like 1000; 1 000 and 1'000. Besides there are different digits used for numbers in different countries Chinese numerals

You can see different numbers in Control Panel -> region and language -> Formats -> additional settings -> Standart digits. (Win 7)

Upvotes: 7

Marek Dzikiewicz
Marek Dzikiewicz

Reputation: 2884

Generally, it's a good practice to always specify explicitly whether you want to use the current culture or if the data your processing is culture invariant. The problem is that if you are processing data which is only processed by your software and not presented to the user in any way (for example database identifiers), then you might run into problems if the data is different on different machines.

For example a database identifier may be serialized on a machine with some culture and deserialized on a machine with a different culture, and in that case it might be different! If you specify explicitly that the string you're processing is culture-invariant, then it will always be the same, regardless of what culture is set on the machine.

You can read more about this topic on MSDN code analysis documentation:

CA1305: Specify IFormatProvider

Upvotes: 1

Related Questions