Enum.GetNames 方法

定义

重载

GetNames(Type)

检索指定枚举中常数名称的数组。

GetNames<TEnum>()

检索指定枚举类型中常数名称的数组。

GetNames(Type)

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

检索指定枚举中常数名称的数组。

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()

参数

enumType
Type

枚举类型。

返回

String[]

enumType 的常数名称的字符串数组。

属性

例外

enumTypenull

enumType 参数不是 Enum

.NET 8 及更高版本: enumType 是一种布尔支持的枚举类型。

示例

下面的示例演示 GetNames 方法的用法。

using namespace System;
enum class Colors
{
   Red, Green, Blue, Yellow
};

enum class Styles
{
   Plaid, Striped, Tartan, Corduroy
};

int main()
{
   Console::WriteLine( "The members of the Colors enum are:" );
   Array^ a = Enum::GetNames( Colors::typeid );
   Int32 i = 0;
   do
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine( o->ToString() );
   }
   while ( ++i < a->Length );

   Console::WriteLine();
   Console::WriteLine( "The members of the Styles enum are:" );
   Array^ b = Enum::GetNames( Styles::typeid );
   i = 0;
   do
   {
      Object^ o = b->GetValue( i );
      Console::WriteLine( o->ToString() );
   }
   while ( ++i < b->Length );
}
// 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
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

注解

返回值数组的元素按枚举常量 (的二进制值排序,即按其无符号数量级) 排序。 下面的示例显示有关 方法为包含负值、零值和正值的枚举返回 GetNames 的数组的信息。

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

如果枚举常量具有相同的值,则未指定其相应名称的顺序。

适用于

GetNames<TEnum>()

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

检索指定枚举类型中常数名称的数组。

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()

类型参数

TEnum

枚举的类型。

返回

String[]

TEnum 的常数名称的字符串数组。

例外

.NET 8 及更高版本: TEnum 是一种布尔支持的枚举类型。

适用于