CA1305: int.Parse (String)

I get warning CA1305.

Microsoft.Globalization: since the behavior of "int.Parse (string)" may vary depending on the current language settings of the user, replace this call in "_Default.CalculateImageButton_Click (object, ImageClickEventArgs) 'with the call" Int.Parse (string, IFormatProvider). If the result is "Int.Parse (string, 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'.

What exactly can go wrong if I skip specifying the culture when parsing Int32?

+4
source share
2 answers

This means that when you read โ€œ1,234โ€ from a data file or database record, try passing it to Int via Parse, you will get 1234 in America and 1 in Germany. The warning gives good advice - if you are interacting with the user, specify CurrentCulture (thanks Andrew!), And if you are interacting with a file system or database (or whatever!), Use InvariantCulture

+9
source

If you parse an integer, it can contain thousands of delimiters, which can be "," or ".". depending on the region.

+1
source

Source: https://habr.com/ru/post/1286116/


All Articles