Why does enum declaration accept short but not Int16
I want to declare a new enum with non-default underlying type. This works:
public enum MyEnum : short
{ A, B, C, }
But I don't under开发者_StackOverflow中文版stand the reason why this doesn't compile:
public enum MyEnum : System.Int16
{ A, B, C, }
Compiler says
Type byte, sbyte, short, ushort, int, uint, long, or ulong expected
I understand that short is an alias for Int16 on all .NET versions (32/64 bit flavors included). I don't see why the compiler gives a different meaning to the alias in that particular case.
Any explanation?
The syntax is correct. C# specification explicitly states that the enum's underlying type must be byte
, sbyte
, short
, ushort
, int
, uint
, long
or ulong
.
Read what Microsoft says about this here.
"...The second example is trying to inherit from a type that derives from System.ValueType, which is strictly prohibited..."
Read here:
精彩评论