Export (0) Print
Expand All

Enum.GetName Method

Retrieves the name of the constant in the specified enumeration that has the specified value.

[Visual Basic]
Public Shared Function GetName( _
   ByVal enumType As Type, _
   ByVal value As Object _
) As String
[C#]
public static string GetName(
 Type enumType,
 object value
);
[C++]
public: static String* GetName(
 Type* enumType,
 Object* value
);
[JScript]
public static function GetName(
   enumType : Type,
 value : Object
) : String;

Parameters

enumType
An enumeration type.
value
The value of a particular enumerated constant in terms of its underlying type.

Return Value

A string containing the name of the enumerated constant in enumType whose value is value, or a null reference (Nothing in Visual Basic) if no such constant is found.

Exceptions

Exception Type Condition
ArgumentNullException enumType or value is a null reference (Nothing in Visual Basic).
ArgumentException enumType is not an Enum.

-or-

value is neither of type enumType nor does it have the same underlying type as enumType.

Example

The following code sample illustrates the use of GetName:

[Visual Basic] 
Imports System

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

[C#] 
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));
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

__value enum Colors { Red, Green, Blue, Yellow };
__value enum Styles { Plaid, Striped, Tartan, Corduroy };

int main() {
    Console::WriteLine("The 4th value of the Colors Enum is {0}", Enum::GetName(__typeof(Colors), __box(3)));
    Console::WriteLine("The 4th value of the Styles Enum is {0}", Enum::GetName(__typeof(Styles), __box(3)));
}

[JScript] 
import System;

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

    public static function Main() {

        Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(Colors, 3));
        Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(Styles, 3));
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Enum Class | Enum Members | System Namespace

Show:
© 2015 Microsoft