While the underlying type of an enum
is an integer (or other discrete numeric type, like long
), there is not much use in treating them as numbers, making bounds-checking a needless operation. As mentioned in Aganju's answer, it makes no sense to think of Dog = Cat + 1
, or Blue = Yellow - 2
. In your example, Item1
and Item2
are related conceptually by whatever commonality they have (return status, shape, color, animal, vehicle type, whatever), but there is no inherent mathematical relation between them.
One time it might come into play is when (de)serializing and you're not using the existing methods to convert your serialized value (e.g., a string) back to an enum. The solution there is not to do bounds-checking, but to use the proper method to do the conversion (i.e., Enum.TryParse<ExampleEnum>(serializedValue, out ExampleEnum enumValue)
). One other case may be if you're interfacing with some (external) non-C# libraries, but even then I fail to see the need for a bounds-check.
Bottom line is that I'd be suspect of situations where your bounds-check fails. Any proper (semi-modern) C# code should not get into that situation, and if it does, you're probably using the wrong type. For example, an enum
when you should be using an int
or an int
when you should be using an enum
.