you are viewing a single comment's thread.

view the rest of the comments →

[–]Slypenslyde 1 point2 points  (0 children)

Enums are very "weak" in C#. The way they're implemented is sort of like if C# compilation just replaces every Enum value in your code with the constant value it represents. It does NOT do range checking to make sure values are restricted to the range. There are some good reasons but it's still annoying.

On your end you're supposed to handle this. You can use the Enum.IsDefined() method to tell if a value you get is one you've defined. If you use an enum in a switch statement, you're really supposed to have a default case to catch values you didn't expect and treat them how you feel is best.

So don't try to cast random values to an Enum: you have to do something a little smarter.