Using Int32.ToString () without formatting and IFormatProvider. Why am I getting CA1305 warning?

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

CA1305: Microsoft. Globalization: since the behavior of 'int.ToString ()' could vary depending on the current user language settings, replace this call in 'Class.Method ()' with a call to 'Int.toString (IFormatProvider). If the result is' int.ToString (IFormatProvider), the user will be displayed, specify "CultureInfo.CurrentCulture" as the "IFormatProvider" Parameter. Otherwise, if the result will be stored and accessible using software, such as when saving to disk or database, specify 'CultureInfo.InvariantCulture'.

This is a very well-known general warning CA1305, which is displayed every time a method is called that has an overload that accepts the IFormatProvider parameter. Although this is an almost correct warning in almost all cases, I cannot think of anything that could go wrong when you call ToString() by default without any format or formatprovider for an integer. So please, if anyone knows anything that might go wrong, enlighten me. I assume IFormatProvider must be a good reason for overloading IFormatProvider .

BTW, I do always make a call using the IFormatProvider overload, because it also seems to have a performance advantage. If anyone has any insightful comments about this, feel free to share them.

+6
c # code-analysis globalization
source share
4 answers

There are things that, as I could imagine, can easily affect the result:

  • Are numbers substituted (not sure if this affects ToString)
  • Whether groups will be grouped (not sure if NumberFormatInfo will ever group numbers into an integer from this type of ToString call only)
  • Negative sign (this can be easily significant)

A short but complete example of how this can 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 
+9
source share

If you look at the NumberFormatInfo Class , you will see that some properties apply to integers, such as PositiveSign or group delimiters for example.

+3
source share

Even if the format does not actually differ between cultures, you will receive a warning that you are actually making a call using cultural information that looks as if it does not contain any cultural information. The warning does not so much care about whether the information about the culture makes any difference, but rather that the code hides the information that the information about the culture is used at all.

The hassle-free ToString call, in turn, will call ToString(CultureInfo.CurrentCulture) . A hassle-free call hides this information, so you have to call where you indicate what kind of culture information she wants to use in the call.

+3
source share

There are things that, as I could imagine, can easily affect the result:

  • Are numbers substituted (not sure if this affects ToString)

  • Are the numbers grouped (not sure if NumberFormatInfo will ever group numbers in integer form only from this type of ToString call)

  • Negative sign (this can be easily significant)

      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()); } } 
0
source share

All Articles