Enum.GetName(Type, Object) Enum.GetName(Type, Object) Enum.GetName(Type, Object) Enum.GetName(Type, Object) Method

定義

指定した値を持つ指定した列挙体にある定数の名前を取得します。Retrieves the name of the constant in the specified enumeration that has the specified value.

public:
 static System::String ^ GetName(Type ^ enumType, System::Object ^ value);
[System.Runtime.InteropServices.ComVisible(true)]
public static string GetName (Type enumType, object value);
static member GetName : Type * obj -> string
Public Shared Function GetName (enumType As Type, value As Object) As String

パラメーター

enumType
Type Type Type Type

列挙型。An enumeration type.

value
Object Object Object Object

基になる型に関連する特定の列挙型定数の値。The value of a particular enumerated constant in terms of its underlying type.

戻り値

値が value である enumType 内にある列挙型定数の名前が含まれた文字列。そのような定数が見つからない場合は、nullA string containing the name of the enumerated constant in enumType whose value is value; or null if no such constant is found.

例外

enumType または valuenull です。enumType or value is null.

enumTypeEnum ではありません。enumType is not an Enum.

または-or- value は型が enumType ではなく、基となる型が enumType と同じでもありません。value is neither of type enumType nor does it have the same underlying type as enumType.

次の例は、GetNameの使用方法を示しています。The following example illustrates the use of 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
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 'Main
End Class 'GetNameTest
' The example displays the following output:
'       The 4th value of the Colors Enum is Yellow
'       The 4th value of the Styles Enum is Corduroy

注釈

複数の列挙メンバーの基になる値が同じである場合、GetNameメソッドは、列挙体のいずれかのメンバーの名前が返されることを保証します。If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. ただし、常に同じ列挙メンバーの名前が返されるとは限りません。However, it does not guarantee that it will always return the name of the same enumeration member. その結果、複数の列挙体メンバーが同じ値を持つ場合、アプリケーションコードは、特定のメンバーの名前を返すメソッドに依存しないようにする必要があります。As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

適用対象