Can enum inheirts from int?  
Author Message
Nickeay





PostPosted: Visual C# Language, Can enum inheirts from int? Top

hi,

I saw some code like this:
public enum HARDWARE_KEYS: int
[
HARDWARE_KEY1 = 193,
HARDWARE_KEY4 = 194,
.....
}

I am just thinking, is there any use that it inherits from int Or what is the good for an enum inheirts from int
Thanks.



Visual C#20  
 
 
louthy





PostPosted: Visual C# Language, Can enum inheirts from int? Top

You can set the int type.  Like short, uint etc.  It just allows you to strongly type the enum and set how much space it takes up in a struct/class.
 
 
Nickeay





PostPosted: Visual C# Language, Can enum inheirts from int? Top

oh, I see,
I had thought in that way, you can do like 1 == HARDWARE_KEY1 directly instead of 1 == (int)HARDWARE_KEY1.
Thanks.


 
 
louthy





PostPosted: Visual C# Language, Can enum inheirts from int? Top

Unfortunately not. I agree that's a little annoying. But technically the 'type' of the enum is HARDWARE_KEYS, not 'int'. But seeing as it's not possible to have anything other than integer values for enumeration members it does seem a little mean-spirited of the compiler to complain about it.