Enum.GetName 方法

定義

多載

GetName(Type, Object)

擷取有指定數值之指定列舉的常數名稱。

GetName<TEnum>(TEnum)

在具有指定數值的指定列舉類型中擷取常數名稱。

GetName(Type, Object)

來源:
Enum.cs
來源:
Enum.cs
來源:
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

就其基礎類型而論的特定列舉常數值。

傳回

字串 (包含值為 valueenumType 中的列舉常數名稱);如果找不到這類常數,則為 null

屬性

例外狀況

enumTypevaluenull

enumType 不是 Enum

-或-

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)

來源:
Enum.cs
來源:
Enum.cs
來源:
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

就其基礎類型而論的特定列舉常數值。

傳回

字串 (包含值為 valueTEnum 中的列舉常數名稱);如果找不到這類常數,則為 null

例外狀況

.NET 8 和更新版本: TEnum 是布林值支援的列舉類型。

適用於