Enum.GetName 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| GetName(Type, Object) |
擷取指定列舉中具有指定值之常數的名稱。 |
| GetName<TEnum>(TEnum) |
擷取指定列舉型別中具有指定值之常數的名稱。 |
GetName(Type, Object)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- 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
特定列舉常數以其底層型別的價值。
傳回
包含列舉常數 enumType 名稱的字串,其值為 value;若 null 找不到此類常數。
- 屬性
例外狀況
enumType 或 value 為 null。
.NET 8 及以後版本:enumType 是一種布林背列舉類型。
範例
下列範例說明 如何使用 GetName。
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
- 來源:
- 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
特定列舉常數以其底層型別的價值。
傳回
包含列舉常數 TEnum 名稱的字串,其值為 value;若 null 找不到此類常數。
例外狀況
.NET 8 及以後版本:TEnum 是一種布林背列舉類型。