I don't understand the purpose of Enum.GetUnderlyingType(Type enumType)
The MSDN documentation doesn't help either:
Returns the underlying type of the specified enumeration.
It seems that this converts a specified type of enum
into... something else. o_O
What IS an underlying type? This looks like some internal details of the implementation. Why is this public? Why would I care about the implementation? Browsing the actual implementation doesn't help either, the method just does some checks then calls
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern Type InternalGetUnderlyingType(Type enumType);
... to which I can't find the source.
Can anybody shed some light on this?
Note that you can specify the underlying type of an enum via
enum Foo : long { One, Two };
And then GetUnderlyingType
is going to return long
for typeof(Foo)
.
Note that the underlying type can be any integral type except for char
types.
Enums are stored in memory as numbers. By default, int32. That's the underlyting type. You can change that:
public enum z : byte {
x = 257 // invalid
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With