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
)

Parameters

enumType
Type: System.Type

An enumeration type.

value
Type: System.Object

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

Return Value

Type: System.String
A string containing the name of the enumerated constant in enumType whose value is value; or null if no such constant is found.

ExceptionCondition
ArgumentNullException

enumType or value is null.

ArgumentException

enumType is not an Enum.

-or-

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

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.

The following example 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));
    }
}
// The example displays the following output: 
//       The 4th value of the Colors Enum is Yellow 
//       The 4th value of the Styles Enum is Corduroy

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft