Enum.GetUnderlyingType(Type) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns the underlying type of the specified enumeration.
public:
static Type ^ GetUnderlyingType(Type ^ enumType);
public static Type GetUnderlyingType (Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static Type GetUnderlyingType (Type enumType);
static member GetUnderlyingType : Type -> Type
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetUnderlyingType : Type -> Type
Public Shared Function GetUnderlyingType (enumType As Type) As Type
Parameters
- enumType
- Type
The enumeration whose underlying type will be retrieved.
Returns
The underlying type of enumType
.
- Attributes
Exceptions
enumType
is null
.
enumType
is not an Enum.
Examples
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
open System
let displayEnumInfo (enumValue: Enum) =
let enumType = enumValue.GetType()
let underlyingType = Enum.GetUnderlyingType enumType
printfn $"{enumValue,-10} {enumType.Name, 18} {underlyingType.Name,15}"
let enumValues: Enum list =
[ ConsoleColor.Red; DayOfWeek.Monday
MidpointRounding.ToEven; PlatformID.Win32NT
DateTimeKind.Utc; StringComparison.Ordinal ]
printfn "%-10s %18s %15s\n" "Member" "Enumeration" "Underlying Type"
for enumValue in enumValues do
displayEnumInfo enumValue
// 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
Remarks
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. 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.
Applies to
See also
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기