With # .NET 4.5, Windows 10, I have the following enumeration:
private enum Enums { A=1, B=2, C=3 }
And this program behaves very strange:
public static void Main() { Enums e; if (Enum.TryParse("12", out e)) { Console.WriteLine("Parsed {0}", e); } else { Console.Write("Not parsed"); } Console.ReadLine(); }
I expect the result of the TryParse method to be false, but to my surprise, the parsing 12 is displayed on the console. In the "Watch" window, it even shows that the value is "12" and it is of type Enums!
This is true for any number line I tried (for example, "540"), but not for lines containing letters ("A12", "12A").
I can easily overcome this by first checking to see if this is a number string, but why is this behavior? Is it for design?
Thanks! Ido
enums c #
Ido cohn
source share