C# Enum GetUnderlyingType

Description

Enum GetUnderlyingType returns the underlying type of the specified enumeration.

Syntax

Enum.GetUnderlyingType has the following syntax.


[ComVisibleAttribute(true)]
public static Type GetUnderlyingType(
  Type enumType
)

Parameters

Enum.GetUnderlyingType has the following parameters.

  • enumType - The enumeration whose underlying type will be retrieved.

Returns

Enum.GetUnderlyingType method returns The underlying type of enumType.

Example

The following example calls the GetUnderlyingType method to display the underlying type of some enumeration members.


using System;/*w  ww  . j  a  v a  2s.com*/

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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version