Search code examples
c#.netcode-analysistostring

.ToString() and .ToString(CultureInfo.CurrentCulture)


I must use .ToString(CultureInfo.CurrentCulture) in every number to string conversion for example. Can I somehow override .ToString() so that I will get no more messages like culture in string conversion explicitly?

For example, now I have to change every

myNumValue.Count.ToString();

to

myNumValue.Count.ToString(CultureInfo.CurrentCulture);

Solution

  • The point of this rule is to make an explicit choice of culture for every formatting and parsing operation. If you try to systematically swap implementations to "trick" the rule, you'll simply end up sweeping potential problems under the rug. As suggested by Lonli-Lokli, you may want to consider using extension methods if you don't like reading ToString calls with a culture arguments, but you should have a distinct extension method for each culture you support. e.g.: ToUIString() -> ToString(CultureInfo.CurrentCulture) and ToInvariantString() -> ToString(CultureInfo.InvariantCulture)