Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get underlying/derived type of enum?

Tags:

c#

How can you get the underlying/derived Type(byte, short, int, etc) of an enum?

like image 505
Will Avatar asked Mar 14 '11 22:03

Will


People also ask

Can you inherit enum?

As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.

Can we inherit enum in C#?

Nope. it is not possible. Enum can not inherit in derived class because by default Enum is sealed.

How do you inherit an enum class?

There is no inheritance with enums. You can instead use classes with named const ints.


1 Answers

You are looking for Enum.GetUnderlyingType(enumType);

Sample from MSDN:

static object GetAsUnderlyingType(Enum enval)
{
    Type entype = enval.GetType();

    Type undertype = Enum.GetUnderlyingType(entype);

    return Convert.ChangeType(enval, undertype);
}
like image 172
BrokenGlass Avatar answered Oct 01 '22 14:10

BrokenGlass