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

33,154

Solution 1

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)

Solution 2

It doesn't make any sense to replace ToString() with ToString(CultureInfo.CurrentCulture) cause this is exactly what ToString() already does.

If you don't put a defined culture (maybe through a user selection) into your code simply stick to the first method.

Solution 3

If you mean a message from Code Analysis, then you can disable a particular rule.

Solution 4

You can create extension method, ToStringEx and call usual method with specified culture.

Share:
33,154
senzacionale
Author by

senzacionale

Updated on July 05, 2022

Comments

  • senzacionale
    senzacionale almost 2 years

    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);