开发者

Using Int32.ToString() without Format and IFormatProvider. Why do I get the CA1305 warning?

开发者 https://www.devze.com 2023-02-22 09:03 出处:网络
I have been wondering about this one for quite some time, but I cannot seem to find a definitive answer. Whenever I convert an integer to a string using the ToString() method, and I run a code analysi

I have been wondering about this one for quite some time, but I cannot seem to find a definitive answer. Whenever I convert an integer to a string using the ToString() method, and I run a code analysis, I get the following warning:

CA1305: Microsoft.Globalization : Because the behavior of 'int.ToString()' could vary based on the current user's locale settings, replace this call in 'Class.Method()' with a call to 'int.ToString(IFormatProvider)'. If the result of 'int.ToString( IFormatProvider)' will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'.

This is the very well-known generic CA1305 warning, which gets shown whenever you make a call to a method that has an overload that accepts an IFormatProvider parameter. While this is a very rightly warning in almost all cases, I can't think of anything that could go wrong when calling the default ToString() without any format or formatprovider on an intege开发者_运维知识库r. So please, if anyone knows of anything that could go wrong, enlighten me. I'm guessing there must be a good reason for the IFormatProvider overload.

By the way, I do always make the call using the IFormatProvider overload, because it also seems to have a performance benefit. If anyone has any insightful comments on this, feel free to share them as well.


There are things which I would imagine could easily affect the result:

  • Whether digits are substituted (not sure if this affects ToString)
  • Whether digits are grouped (not sure if NumberFormatInfo will ever group digits in an integer just from this sort of ToString call)
  • The negative sign (this could easily be significant)

Short but complete example of how it could affect things, using the NegativeSign property:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()
    {
        int x = -10;
        Console.WriteLine(x.ToString());

        CultureInfo culture = Thread.CurrentThread.CurrentCulture;
        // Make a writable clone
        culture = (CultureInfo) culture.Clone();
        culture.NumberFormat.NegativeSign = "!!!";

        Thread.CurrentThread.CurrentCulture = culture;
        Console.WriteLine(x.ToString());
    }
}

Output:

-10
!!!10


If you take a look at the NumberFormatInfo Class, you will see that some properties apply to integers, such as PositiveSign or Group Separators for example.


Even if the format actually doesn't differ between cultures, you get the warning beacuse you are actually doing a call using culture information that looks like it doesn't contain any culture information. The warning isn't so much concerned about whether the culture information makes any difference, rather that the code hides the information that culture information is used at all.

The parameterless call to ToString will in turn make a call to ToString(CultureInfo.CurrentCulture). The parameterless call hides this information, so you should make the call where you show which culture information it is that you want to use in the call.

0

精彩评论

暂无评论...
验证码 换一张
取 消