Jon Skeet said:
The boxed type (the reference type) representing any enum is derived
from System.Enum. The value type doesn't.
There is no value type of enum. There is an underlying TypeCode that is
used for integral representation of the enum. To get the value type, you
must unbox it. If it were already an int, it wouldn't be necessary to cast
an int as an int. But if you box an int, it is necessary to unbox to get
back to the int. The syntax for unboxing is, coincidentally, the same as
the syntax for an explicit cast.
It's exactly the same as System.Int32 deriving from System.ValueType,
despite System.ValueType being a reference type in itself. That doesn't
mean that ints always need to be boxed and unboxed - they only do when
there's a conversion to System.ValueType or System.Object.
System.Int32 is a value type inheriting from System.ValueType. System.Enum
is a reference type inheriting from System.ValueType. Since we seem to
agree that System.Enum is a reference type, yet it inherits from a value
type, then how did it get to be a reference type? As I first quoted from
the C# specification, it is boxed at runtime. That's the only way to get a
reference type out of a value type.
Only if you actually *have* an expression of type Enum rather than of
the specific enum type in the first place.
All specific enum instances are of type Enum. Subclasses are always of the
type of the superclass. Basic OOP.
Again, from the C# specification:
"an unboxing conversion (Section 4.3.2) exists from System.Enum to any enum
type."
Also from the C# specification:
<quote>
The explicit enumeration conversions are:
[...]
From any enum-type to sbyte, byte, short, ushort, int, uint, long,
ulong, char, float, double, or decimal.
</quote>
There are a lot of explicit conversions possible in the .Net classes. They
do not imply inheritance by the existence of the explicit conversion. In
fact, since a subclass can always be substituted for a superclass, the
opposite could be inferred though it doesn't always hold true.
No mention of boxing there, because there's no need for one.
Not entirely sure what you mean here, but I believe you're wrong if
you're basing that statement on the idea that enum values are always
boxed.
Well, there has been argument that enum instances do not inherit from
System.Enum. Again, if they do not, then how do they get the instance
methods of System.Enum? They do not get the instance methods of
System.Int32.
The language specification *could* have been written such that the
enumeration conversions were implicit rather than explicit - but type
safety would have been lost.
But it wasn't.
Dale