Enum.GetNames(Type) Enum.GetNames(Type) Enum.GetNames(Type) Enum.GetNames(Type) Method

定義

指定した列挙体に含まれている定数の名前の配列を取得します。Retrieves an array of the names of the constants in a specified enumeration.

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

パラメーター

enumType
Type Type Type Type

列挙型。An enumeration type.

戻り値

String[]

enumType に含まれている定数の名前の文字列配列。A string array of the names of the constants in enumType.

例外

enumType パラメーターが Enum ではありません。enumType parameter is not an Enum.

GetNames メソッドの使用例を次に示します。The following example illustrates the use of the GetNames method.

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

注釈

戻り値の配列の要素は列挙定数のバイナリ値で並べ替えられます (つまり、その符号なしの大きさによって)。The elements of the return value array are sorted by the binary values of the enumerated constants (that is, by their unsigned magnitude). 次の例は、によって返される配列に関する情報を表示、GetNames列挙体は、負の値、0、正の値を含むメソッド。The following example provides displays information about the array returned by the GetNames method for an enumeration that includes a negative, zero, and a positive value.

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

同じ値を持つ列挙型の定数がある場合は、対応する名前の順序は指定されていません。If there are enumerated constants with same value, the order of their corresponding names is unspecified.

適用対象