0

For C# floating points types, I understand that one "culture effect" is that ',' may be used instead of '.', but for integers types what are the possible effects?

I ask since

 Int32.TryParse() 

will accept a CultureInfo.InvariantCulture (IFormatProvider) argument but

 (T)Enum.Parse(typeof(T), enumString, true);

does not even though Enum.Parse() accepts integers or the enum literal.

Example: A client uses Int32.ToString() with one culture and a server uses Enum.Parse() to with a different culture (current since there is no arg).

crokusek
  • 5,345
  • 3
  • 43
  • 61
  • 1
    Integers have seperators too. i.e. `1 000 000` or `1,000,000`. I imagine there are others. – Ash Burlaczenko Jul 26 '13 at 21:36
  • I personnally don't use Enum.Parse for integer values. You can use int.Parse for that and a cast. I didn't even know it would work, but then I think it's unreliable culture-wise. In french, we use a single quote ''' as a thousand separator, whereas it's usually a comma ',' in English. Those kind of things would probably fail. – jods Jul 26 '13 at 21:38

1 Answers1

1

The documentation is not clear on what the actual behavior is, so I looked at the source code for Enum.Parse (in .NET 4.5). It will always use an invariant culture (not the current culture) when parsing integer values from the input string. Specifically the line to parse the integer is:

temp = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);

Numeric values are identified by checking if the first character is a digit (using Char.IsDigit), '+', or '-'. So you would be safe if you convert to a string using the invariant culture on the client and use Enum.Parse on the server and they both use .NET 4.5. However, I'm not sure as to whether this is the behavior on older versions of the framework or is guaranteed for future versions.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122