Edit: Comments at bottom. Also, this.
Here\'s what\'s kind of confusing me. My understanding is that if I have an enum like this...
Extracted from MSDN:
The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
So, the cast is possible, but you need to force it:
The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type.
When you box your enum into object
, the animal object is derived from System.Enum
(the real type is known at runtime) so it's actually an int
, so the cast is valid.
(animal is Enum)
returns true
: For this reason you can unbox animal into an Enum or event into an int doing an explicit casting.(animal is int)
returns false
: The is
operator (in general type check) does not check the underlying type for Enums. Also, for this reason you need to do an explicit casting to convert Enum to int.