I have a enum Color, it may derive from long or byte or int, and I want to know what type does it really derive from, long or byte or int? In the process, I have met 2 problems.
First, the Color is defined like below:
enum Color : long
{
red = 1,
black = 2,
blue = 3
}
I write the code below to do such things:
Console.WriteLine(typeof(Color)); // ConsoleApp7.Color
Console.WriteLine(typeof(Color).BaseType); // System.Enum
At this moment, I met my first problem: it's curious that typeof(Color).BaseType is System.Enum, because Color is a enum type.
So I'm wondering that whether you defining a enum type like enum Enum1 { ... }, it actually means that: class Enum1 : enum { ... }?
Based on problem 1, if I want to get its real base type, I need to write:
Console.WriteLine(typeof(Color).BaseType.BaseType);
Its output is System.ValueType, we know that int, byte and long all are System.ValueType, how can I get the keyword long?
Thanks.
You want an Underlying type, not Base type:
var underlyingType = Enum.GetUnderlyingType(typeof(ConsoleColor))
Also note that any specific enum (like your Color) is a value type and base type of System.Enum is System.ValueType (despite the fact that System.Enum is a reference type). That's why your typeof(Color).BaseType.BaseType equals System.ValueType
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