Enum.GetUnderlyingType(Type) Enum.GetUnderlyingType(Type) Enum.GetUnderlyingType(Type) Enum.GetUnderlyingType(Type) Method

定義

指定した列挙体の基になる型を返します。Returns the underlying type of the specified enumeration.

public:
 static Type ^ GetUnderlyingType(Type ^ enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static Type GetUnderlyingType (Type enumType);
static member GetUnderlyingType : Type -> Type
Public Shared Function GetUnderlyingType (enumType As Type) As Type

パラメーター

enumType
Type Type Type Type

基になる型が取得される列挙。The enumeration whose underlying type will be retrieved.

戻り値

enumType の基になる型。The underlying type of enumType.

例外

enumTypeEnum ではありません。enumType is not an Enum.

次の例では、GetUnderlyingTypeメソッドを呼び出して、列挙型のメンバーの基になる型を表示しています。The following example calls the GetUnderlyingType method to display the underlying type of some enumeration members.

using System;

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

   static void DisplayEnumInfo(Enum enumValue)
   {
      Type enumType = enumValue.GetType();
      Type underlyingType = Enum.GetUnderlyingType(enumType);
      Console.WriteLine("{0,-10} {1, 18}   {2,15}",
                        enumValue, enumType.Name, underlyingType.Name);   
   }
}
// The example displays the following output:
//       Member            Enumeration   Underlying Type
//       
//       Red              ConsoleColor             Int32
//       Monday              DayOfWeek             Int32
//       ToEven       MidpointRounding             Int32
//       Win32NT            PlatformID             Int32
//       Utc              DateTimeKind             Int32
//       Ordinal      StringComparison             Int32
Module Example
   Public Sub Main()
      Dim enumValues() As [Enum] = { ConsoleColor.Red, DayOfWeek.Monday, 
                                     MidpointRounding.ToEven, PlatformID.Win32NT, 
                                     DateTimeKind.Utc, StringComparison.Ordinal }
      Console.WriteLine("{0,-10} {1, 18}   {2,15}", 
                        "Member", "Enumeration", "Underlying Type")
      Console.WriteLine()
      For Each enumValue In enumValues
         DisplayEnumInfo(enumValue)
      Next
   End Sub

   Sub DisplayEnumInfo(enumValue As [Enum])
      Dim enumType As Type = enumValue.GetType()
      Dim underlyingType As Type = [Enum].GetUnderlyingType(enumType)
      Console.WriteLine("{0,-10} {1, 18}   {2,15}",
                        enumValue, enumType.Name, underlyingType.Name)   
   End Sub
End Module
' The example displays the following output:
'       Member            Enumeration   Underlying Type
'       
'       Red              ConsoleColor             Int32
'       Monday              DayOfWeek             Int32
'       ToEven       MidpointRounding             Int32
'       Win32NT            PlatformID             Int32
'       Utc              DateTimeKind             Int32
'       Ordinal      StringComparison             Int32

注釈

Enum構造体を使用すると、値を名前付き定数として表すことができます。The Enum structure enables values to be represented as named constants. 列挙値のデータ型は、基になる型と呼ばれます。The data type of the enumeration's values is known as its underlying type. たとえば、 DayOfWeek列挙型の基になる型は、曜日 (DayOfWeek.MondayDayOfWeek.Tuesdayなど) を表す定数で構成されていて、Int32です。For example, the underlying type of the DayOfWeek enumeration, which consists of constants that represent each day of the week (DayOfWeek.Monday, DayOfWeek.Tuesday, and so on), is Int32.

適用対象

こちらもご覧ください