22
votes

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 understand 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?

2
I remember a similar question getting closed yesterday - V4Vendetta
I didn't find it despite several minutes spent on reading search results :) - Johann Blais
@Daniel Daranas, sorry I searched using int16 as a keyword... it might explain why I didn't find them - Johann Blais
@Johann Blais, my search was this: google.com/… - Daniel Daranas

2 Answers

19
votes

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.

4
votes

"...The second example is trying to inherit from a type that derives from System.ValueType, which is strictly prohibited..."

Read here: