C# .NET 4.5, Windows 10, I have the following enum:
private enum Enums
{
A=1, B=2, C=3
}
And this program behaves in a very strange way:
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 would expect the result of the TryParse method to be false, but to my surprise the console shows "Parsed 12". In the Watch window it even shows that the value is "12" and it is of the Enums type!
This is true for any number string that I tried (e.g. "540"), but not for strings that include letters ("A12", "12A").
I can easily overcome this by first checking if it's a number-only string, but why is this the behaviour? Is it by design?
Thanks! Ido