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이 아닌 경우

또는

valueenumType 형식이 아니며 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 부울 지원 열거형 형식입니다.

적용 대상