Enum.GetName メソッド

定義

オーバーロード

GetName(Type, Object)

指定した値を持つ指定した列挙体にある定数の名前を取得します。

GetName<TEnum>(TEnum)

指定した値を持つ指定した列挙型に含まれている定数の名前を取得します。

GetName(Type, Object)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

指定した値を持つ指定した列挙体にある定数の名前を取得します。

public:
 static System::String ^ GetName(Type ^ enumType, System::Object ^ value);
public static string GetName (Type enumType, object value);
public static string? GetName (Type enumType, object value);
[System.Runtime.InteropServices.ComVisible(true)]
public static string GetName (Type enumType, object value);
static member GetName : Type * obj -> string
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetName : Type * obj -> string
Public Shared Function GetName (enumType As Type, value As Object) As String

パラメーター

enumType
Type

列挙型。

value
Object

基になる型に関連する特定の列挙型定数の値。

戻り値

値が value である enumType 内にある列挙型定数の名前が含まれた文字列。そのような定数が見つからない場合は、null

属性

例外

enumType または valuenull です。

enumTypeEnum ではありません。

- または -

value は型が enumType ではなく、基となる型が enumType と同じでもありません。

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

次の例は、GetNameの使用方法を示しています。

using namespace System;

enum class Colors
{
   Red, Green, Blue, Yellow
};

enum class Styles
{
   Plaid, Striped, Tartan, Corduroy
};

int main()
{
   Console::WriteLine(  "The 4th value of the Colors Enum is {0}", Enum::GetName( Colors::typeid, 3 ) );
   Console::WriteLine(  "The 4th value of the Styles Enum is {0}", Enum::GetName( Styles::typeid, 3 ) );
}
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy
using System;

public class GetNameTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid, Striped, Tartan, Corduroy };

    public static void Main() {

        Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
        Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
    }
}
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy
open System

type Colors =
    | Red = 0
    | Green = 1
    | Blue = 2
    | Yellow = 3

type Styles =
    | Plaid = 0
    | Striped = 1
    | Tartan = 2
    | Corduroy = 3

printfn $"The 4th value of the Colors Enum is {Enum.GetName(typeof<Colors>, 3)}"
printfn $"The 4th value of the Styles Enum is {Enum.GetName(typeof<Styles>, 3)}"
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy
Public Class GetNameTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 'Colors
    
    Enum Styles
        Plaid
        Striped
        Tartan
        Corduroy
    End Enum 'Styles
    
    Public Shared Sub Main() 
        Console.WriteLine("The 4th value of the Colors Enum is {0}", [Enum].GetName(GetType(Colors), 3))
        Console.WriteLine("The 4th value of the Styles Enum is {0}", [Enum].GetName(GetType(Styles), 3))
    End Sub
End Class
' The example displays the following output:
'       The 4th value of the Colors Enum is Yellow
'       The 4th value of the Styles Enum is Corduroy

注釈

複数の列挙メンバーの基になる値が同じである場合、GetNameメソッドは、列挙体のいずれかのメンバーの名前が返されることを保証します。 ただし、常に同じ列挙メンバーの名前を返す保証はありません。 その結果、複数の列挙メンバーが同じ値を持つ場合、アプリケーション コードは特定のメンバーの名前を返すメソッドに依存しないでください。

適用対象

GetName<TEnum>(TEnum)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

指定した値を持つ指定した列挙型に含まれている定数の名前を取得します。

public:
generic <typename TEnum>
 where TEnum : value class static System::String ^ GetName(TEnum value);
public static string? GetName<TEnum> (TEnum value) where TEnum : struct;
static member GetName : 'Enum -> string (requires 'Enum : struct)
Public Shared Function GetName(Of TEnum As Structure) (value As TEnum) As String

型パラメーター

TEnum

列挙体の型。

パラメーター

value
TEnum

基になる型に関連する特定の列挙型定数の値。

戻り値

値が value である TEnum 内にある列挙型定数の名前が含まれた文字列。そのような定数が見つからない場合は、null

例外

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

適用対象