简体   繁体   中英

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?

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM