英語で読む

次の方法で共有


Enum.GetNames メソッド

定義

オーバーロード

GetNames(Type)

指定した列挙体に含まれている定数の名前の配列を取得します。

GetNames<TEnum>()

指定した列挙型に含まれている定数の名前の配列を取得します。

GetNames(Type)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs

指定した列挙体に含まれている定数の名前の配列を取得します。

C#
public static string[] GetNames(Type enumType);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static string[] GetNames(Type enumType);

パラメーター

enumType
Type

列挙型。

戻り値

String[]

enumType に含まれている定数の名前の文字列配列。

属性

例外

enumTypenullです。

enumType パラメーターが Enum ではありません。

.NET 8 以降のバージョン: enumType は、ブール型に基づく列挙型です。

GetNames メソッドの使用例を次に示します。

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

注釈

戻り値配列の要素は、列挙定数のバイナリ値 (つまり、符号なしの大きさ) で並べ替えられます。 次の例では、負の値、ゼロ、および正の値を含む列挙体に対して、GetNamesメソッドによって返される配列に関する情報を表示します。

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

同じ値を持つ列挙定数がある場合、対応する名前の順序は不定です。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetNames<TEnum>()

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs

指定した列挙型に含まれている定数の名前の配列を取得します。

C#
public static string[] GetNames<TEnum>() where TEnum : struct;

型パラメーター

TEnum

列挙体の型。

戻り値

String[]

TEnum に含まれている定数の名前の文字列配列。

例外

.NET 8 以降のバージョン: TEnum は、ブール型に基づく列挙型です。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET 5, 6, 7, 8, 9, 10