2

I have the following Enum (which is generated from a xsd):

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.ebutilities.at/invoice/02p00")]
[System.Xml.Serialization.XmlRootAttribute("Sector", Namespace = "http://www.ebutilities.at/invoice/02p00", IsNullable = false)]public enum    
SectorType
{
    [System.Xml.Serialization.XmlEnumAttribute("01")]
    Item01,
    [System.Xml.Serialization.XmlEnumAttribute("02")]
    Item02,
    [System.Xml.Serialization.XmlEnumAttribute("03")]
    Item03,
    [System.Xml.Serialization.XmlEnumAttribute("04")]
    Item04,
    [System.Xml.Serialization.XmlEnumAttribute("05")]
    Item05,
    [System.Xml.Serialization.XmlEnumAttribute("06")]
    Item06,
    [System.Xml.Serialization.XmlEnumAttribute("07")]
    Item07,
    [System.Xml.Serialization.XmlEnumAttribute("08")]
    Item08,
    [System.Xml.Serialization.XmlEnumAttribute("09")]
    Item09,
    [System.Xml.Serialization.XmlEnumAttribute("99")]
    Item99,
}

So I want to parse a string to SectorType:

string s = "88";
SectorType sectorType;
bool result = Enum.TryParse(s, out sectorType);

After this my sectorType is "88" and result is true. So the conversion succeeded. Also this is working fine:

SectorType sectorType = (SectorType)Enum.Parse(typeof (SectorType), "88")

Value of sectorType is 88.

Here's a picture from the debugger:

enter image description here

MSDN provides following information:

Enum.TryParse Method

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.

Obviously there is no equivalent enumerated object (88 (or whatever number) != Item01,..., Item09, Item99).

I was thinking Enums are strongly-typed (see dotnetperls/enums). It says:

We see that enums are strongly-typed. You cannot assign them to just any value.

But clearly in my example, i could assign any number to my SectorType-Enum and I really don't know why this is working...

See it running at .NET Fiddle.

Community
  • 1
  • 1
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
  • Also note that your XML integer values will be different from your .NET integer values. `Enum.TryParse` only works on the latter. – leppie Feb 09 '15 at 10:50
  • Can someone reopen the issue? I don't think https://stackoverflow.com/questions/6741649/enum-tryparse-returns-true-for-any-numeric-values is the same question as this one. This enum case is special because enum value cannot start with digits. – Tony Qu Feb 04 '22 at 18:57

1 Answers1

2

From the MSDN page for Enum.TryParse<TEnum>(string value, ...):

If value is the string representation of an integer that does not represent an underlying value of the TEnum enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum.

H H
  • 263,252
  • 30
  • 330
  • 514