Enum.GetName Metodo

Definizione

Overload

GetName(Type, Object)

Restituisce il nome della costante nell'enumerazione del valore specificato.

GetName<TEnum>(TEnum)

Recupera il nome della costante nel tipo di enumerazione del valore specificato.

GetName(Type, Object)

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

Restituisce il nome della costante nell'enumerazione del valore specificato.

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

Parametri

enumType
Type

Tipo di enumerazione.

value
Object

Valore di una determinata costante enumerata in base al relativo tipo sottostante.

Restituisce

Stringa che contiene il nome della costante enumerata in enumType il cui valore è value oppure null se tale costante non viene trovata.

Attributi

Eccezioni

enumType o value è null.

enumType non è un elemento Enum.

-oppure-

value non è di tipo enumType e non ha lo stesso tipo sottostante di enumType.

.NET 8 e versioni successive: enumType è un tipo di enumerazione con supporto booleano.

Esempio

Nell'esempio seguente viene illustrato l'uso di 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

Commenti

Se più membri di enumerazione hanno lo stesso valore sottostante, il GetName metodo garantisce che restituirà il nome di uno di questi membri di enumerazione. Tuttavia, non garantisce che restituisca sempre il nome dello stesso membro di enumerazione. Di conseguenza, quando più membri di enumerazione hanno lo stesso valore, il codice dell'applicazione non deve mai dipendere dal metodo che restituisce il nome di un membro specifico.

Si applica a

GetName<TEnum>(TEnum)

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

Recupera il nome della costante nel tipo di enumerazione del valore specificato.

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

Parametri di tipo

TEnum

Tipo dell'enumerazione.

Parametri

value
TEnum

Valore di una determinata costante enumerata in base al relativo tipo sottostante.

Restituisce

Stringa che contiene il nome della costante enumerata in TEnum il cui valore è value oppure null se tale costante non viene trovata.

Eccezioni

.NET 8 e versioni successive: TEnum è un tipo di enumerazione con supporto booleano.

Si applica a