I'm running a beta version of ReSharper, and it's giving me warnings for the following code:
int id;
// ...
DoSomethingWith(id.ToString());
The warning is on the id.ToString()
call, and it's telling me "Specify a culture in string conversion explicitly". I understand the warning, and I know how to fix it -- just change the code to the much more unwieldy id.ToString(CultureInfo.InvariantCulture)
.
But my question is: is that necessary? I mean, obviously it's important to specify the culture when you're using types like DateTime
(different cultures have different date formats) and Double
(different characters used for the decimal point). But Int32.ToString()
, at least in the en-US and invariant cultures, doesn't add any formatting at all. No commas, no decimal points, no dollar signs, nothing. So what would there be to vary by culture?
Are there some cultures that actually add some sort of formatting when you call the parameterless Int32.ToString()
? Or is this a bug in the ReSharper beta, and this warning really isn't applicable to Int32
(in which case I'll file a ReSharper bug report)?
id.ToString(CultureInfo.CurrentCulture)
. I recommend two extension methods be defined for all types you care about:ToDisplayString()
andToInvariantString()
. Then no longer think about the type you have, just the purpose for which you are making the string. Self-documented code. – Serous