Enum.GetNames Método

Definição

Sobrecargas

Name Description
GetNames(Type)

Recupera um array com os nomes das constantes numa enumeração especificada.

GetNames<TEnum>()

Recupera um array com os nomes das constantes num tipo de enumeração especificado.

GetNames(Type)

Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs

Recupera um array com os nomes das constantes numa enumeração especificada.

public:
 static cli::array <System::String ^> ^ GetNames(Type ^ enumType);
public static string[] GetNames(Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static string[] GetNames(Type enumType);
static member GetNames : Type -> string[]
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetNames : Type -> string[]
Public Shared Function GetNames (enumType As Type) As String()

Parâmetros

enumType
Type

Um tipo de enumeração.

Devoluções

String[]

Um array de cadeias dos nomes das constantes em enumType.

Atributos

Exceções

enumType é null.

enumType parâmetro não é um Enum.

.NET 8 e versões posteriores: enumType é um tipo de enumeração suportado por Boole.

Exemplos

O exemplo seguinte ilustra a utilização do GetNames método.

using System;

public class GetNamesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid, Striped, Tartan, Corduroy };

    public static void Main() {

        Console.WriteLine("The members of the Colors enum are:");
        foreach(string s in Enum.GetNames(typeof(Colors)))
            Console.WriteLine(s);

        Console.WriteLine();

        Console.WriteLine("The members of the Styles enum are:");
        foreach(string s in Enum.GetNames(typeof(Styles)))
            Console.WriteLine(s);
    }
}
// The example displays the following output:
//       The members of the Colors enum are:
//       Red
//       Green
//       Blue
//       Yellow
//
//       The members of the Styles enum are:
//       Plaid
//       Striped
//       Tartan
//       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 members of the Colors enum are:"
for s in Enum.GetNames typeof<Colors> do
    printfn $"{s}"

printfn "\nThe members of the Styles enum are:"
for s in Enum.GetNames typeof<Styles> do
    printfn $"{s}"
// The example displays the following output:
//       The members of the Colors enum are:
//       Red
//       Green
//       Blue
//       Yellow
//
//       The members of the Styles enum are:
//       Plaid
//       Striped
//       Tartan
//       Corduroy
Public Class GetNamesTest
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 
    
    Enum Styles
        Plaid
        Striped
        Tartan
        Corduroy
    End Enum
    
    Public Shared Sub Main()
        
        Console.WriteLine("The members of the Colors enum are:")
        For Each s In [Enum].GetNames(GetType(Colors))
            Console.WriteLine(s)
        Next

        Console.WriteLine()
        
        Console.WriteLine("The members of the Styles enum are:")
        For Each s In [Enum].GetNames(GetType(Styles))
            Console.WriteLine(s)
        Next
    End Sub
End Class
' The example displays the following output:
'       The members of the Colors enum are:
'       Red
'       Green
'       Blue
'       Yellow
'       
'       The members of the Styles enum are:
'       Plaid
'       Striped
'       Tartan
'       Corduroy

Observações

Os elementos do array de valores de retorno são ordenados pelos valores binários das constantes enumeradas (isto é, pela sua magnitude sem sinal). O exemplo seguinte apresenta informações sobre o array devolvido pelo GetNames método para uma enumeração que inclui um valor negativo, zero e positivo.

using System;

enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };

public class Example
{
   public static void Main()
   {
      foreach (var name in Enum.GetNames(typeof(SignMagnitude))) {
         Console.WriteLine("{0,3:D}     0x{0:X}     {1}",
                           Enum.Parse(typeof(SignMagnitude), name),
                           name);
}   }
}
// The example displays the following output:
//         0     0x00000000     Zero
//         1     0x00000001     Positive
//        -1     0xFFFFFFFF     Negative
open System

type SignMagnitude =
   | Negative = -1
   | Zero = 0
   | Positive = 1

for name in Enum.GetNames typeof<SignMagnitude> do
    let p = Enum.Parse(typeof<SignMagnitude>, name)
    printfn $"{p,3:D}     0x{p:X}     {name}"

// The example displays the following output:
//         0     0x00000000     Zero
//         1     0x00000001     Positive
//        -1     0xFFFFFFFF     Negative
Public Enum SignMagnitude As Integer
   Negative = -1 
   Zero = 0
   Positive = 1
End Enum
   
Module Example
   Public Sub Main()
      Dim names() As String = [Enum].GetNames(GetType(SignMagnitude))
      For Each name In names
         Console.WriteLine("{0,3:D}     0x{0:X}     {1}", 
                           [Enum].Parse(GetType(SignMagnitude), name), 
                           name)
      Next
   End Sub
End Module
' The example displays the following output:
'      0     0x00000000     Zero
'      1     0x00000001     Positive
'     -1     0xFFFFFFFF     Negative

Se existirem constantes enumeradas com o mesmo valor, a ordem dos seus nomes correspondentes não é especificada.

Aplica-se a

GetNames<TEnum>()

Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs
Origem:
Enum.cs

Recupera um array com os nomes das constantes num tipo de enumeração especificado.

public:
generic <typename TEnum>
 where TEnum : value class static cli::array <System::String ^> ^ GetNames();
public static string[] GetNames<TEnum>() where TEnum : struct;
static member GetNames : unit -> string[] (requires 'Enum : struct)
Public Shared Function GetNames(Of TEnum As Structure) () As String()

Parâmetros de Tipo Genérico

TEnum

O tipo da enumeração.

Devoluções

String[]

Um array de cadeias dos nomes das constantes em TEnum.

Exceções

.NET 8 e versões posteriores: TEnum é um tipo de enumeração suportado por Boole.

Aplica-se a