Enum.GetName Metoda

Definicja

Przeciążenia

GetName(Type, Object)

Pobiera nazwę stałej w określonym wyliczenie, które ma określoną wartość.

GetName<TEnum>(TEnum)

Pobiera nazwę stałej w określonym typie wyliczania, który ma określoną wartość.

GetName(Type, Object)

Źródło:
Enum.cs
Źródło:
Enum.cs
Źródło:
Enum.cs

Pobiera nazwę stałej w określonym wyliczenie, które ma określoną wartość.

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

Parametry

enumType
Type

Typ wyliczenia.

value
Object

Wartość określonej wyliczonej stałej pod względem jego typu bazowego.

Zwraca

Ciąg zawierający nazwę wyliczonej stałej w enumType wartości value; lub null jeśli taka stała nie zostanie znaleziona.

Atrybuty

Wyjątki

enumType lub value to null.

enumType nie jest elementem Enum.

-lub-

value nie ma typu enumType ani nie ma tego samego typu bazowego co enumType.

.NET 8 i nowsze wersje: enumType jest typem wyliczania opartego na wartości logicznej.

Przykłady

Poniższy przykład ilustruje użycie metody 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

Uwagi

Jeśli wiele elementów członkowskich wyliczenia ma taką samą wartość bazową, GetName metoda gwarantuje, że zwróci nazwę jednego z tych elementów członkowskich wyliczenia. Nie gwarantuje jednak, że zawsze zwróci nazwę tego samego elementu członkowskiego wyliczenia. W rezultacie, gdy wiele elementów członkowskich wyliczenia ma taką samą wartość, kod aplikacji nigdy nie powinien zależeć od metody zwracającej nazwę określonego elementu członkowskiego.

Dotyczy

GetName<TEnum>(TEnum)

Źródło:
Enum.cs
Źródło:
Enum.cs
Źródło:
Enum.cs

Pobiera nazwę stałej w określonym typie wyliczania, który ma określoną wartość.

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

Parametry typu

TEnum

Typ wyliczenia.

Parametry

value
TEnum

Wartość określonej wyliczonej stałej pod względem jego typu bazowego.

Zwraca

Ciąg zawierający nazwę wyliczonej stałej w TEnum wartości value; lub null jeśli taka stała nie zostanie znaleziona.

Wyjątki

.NET 8 i nowsze wersje: TEnum jest typem wyliczania opartego na wartości logicznej.

Dotyczy