Export (0) Print
Expand All

Enum.GetName Method

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public static string GetName (
	Type enumType,
	Object value
)
/** @attribute ComVisibleAttribute(true) */ 
public static String GetName (
	Type enumType, 
	Object value
)
ComVisibleAttribute(true) 
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.

Exception typeCondition

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.

The following code sample illustrates the use of GetName :

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));
    }
}

import System.*;

public class GetNameTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    }//Colors

    enum Styles
    {
        plaid (0),
        striped (1),
        tartan (2),
        corduroy (3);
    }//Styles

    public static void main(String[] args)
    {
        Console.WriteLine("The 4th value of the Colors Enum is {0}", 
            Enum.GetName(Colors.class.ToType(), (Int32)3));
        Console.WriteLine("The 4th value of the Styles Enum is {0}", 
            Enum.GetName(Styles.class.ToType(), (Int32)3));
    } //main
} //GetNameTest

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));
    }
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft