Get the underlying type of the specified enumeration in CSharp

Description

The following code shows how to get the underlying type of the specified enumeration.

Example


using System;//  www .j av  a2  s .c o m

public class Example
{
   public static void Main()
   {
      Enum[] enumValues = { ConsoleColor.Red, DayOfWeek.Monday, 
                            MidpointRounding.ToEven, PlatformID.Win32NT, 
                            DateTimeKind.Utc, StringComparison.Ordinal };
      foreach (var enumValue in enumValues){
          Type enumType = enumValue.GetType();
          Type underlyingType = Enum.GetUnderlyingType(enumType);
          Console.WriteLine("{0,-10} {1, 18}   {2,15}",
                            enumValue, enumType.Name, underlyingType.Name);   
        
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var