tags:

views:

110

answers:

4

By default, C# enums are stored as integers. I'd like to make it a short instead. Is there a way to do this?

+7  A: 

sure, this can be done, but it has to be an integral type ( byte, short, int, etc.) except char...

enum myEnum : short
{
    FirstValue = 0,
};

here is the MSDN docs

Muad'Dib
+1 for beating @RPM1984 by half a minute... lol
rockinthesixstring
I blame my office's internet connection. =)
RPM1984
@rockinthesixstring what else would you expect from the Kwisatz Haderach? ;)
Muad'Dib
@Maud'Dib - i think char is the exception. i dont think it is allowed. http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx
InSane
that is right, any integral type EXCEPT char
Muad'Dib
Heh thanks, much easier than I thought it'd be! I actually read the doc but didn't scroll down far enough to see the part where uses the inheritance.
Daniel T.
+2  A: 

Like this:

enum MyEnum : short
{
  ...
}
RPM1984
A: 

Sometimes it makes more sense to have String values as enums you can use Attributes and achieve this check this link http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx

Kiran Bheemarti
A: 

yes you can create enum like

enum Range : short {Max = 6, Min = 1, Mid = 3};
hemant