c # – Use of foreign enumeration members for limit / validity testing

While the underlying type one enum is an integer (or some other discrete numeric type, such as Long), it does not make much sense to treat them as numbers, making the boundary check an unnecessary operation. As mentioned in Aganju's answer, it makes no sense to think about it Dog = cat + 1, or Blue = Yellow - 2, In your example Item 1 and item2 are conceptually interconnected by the similarities (return status, shape, color, animal, vehicle type, whatever), but there is no inherent mathematical relationship between them.

Once it could It comes into play when you (de) serialize and you do not use the existing methods to convert your serialized value (such as a string) back to an enumeration. The solution is not to run a bounding check, but to use the correct method for the conversion (ie. Enum.TryParse(serializedValue, from ExampleEnum enumValue) ). Another case might be when you are working with some (external) non-C # libraries, but even then I see no need for a bounding check.

The bottom line is that I have a suspicion in situations where your bound-check fails. Any correct (semi-modern) C # code should not get into this situation, and if it does, you're probably using the wrong type. For example, one enum when should you use one int or a int when should you use one enum,