Johann Blais
Johann Blais

Reputation: 9469

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

Upvotes: 23

Views: 18699

Answers (2)

Daniel Daranas
Daniel Daranas

Reputation: 22624

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.

Upvotes: 21

danyolgiax
danyolgiax

Reputation: 13086

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

Read here:

Upvotes: 4

Related Questions