Edit

Share via


Enum.GetName Method

Definition

Overloads

GetName(Type, Object)

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

GetName<TEnum>(TEnum)

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

GetName(Type, Object)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

C#
public static string GetName(Type enumType, object value);
C#
public static string? GetName(Type enumType, object value);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static string GetName(Type enumType, object value);

Parameters

enumType
Type

An enumeration type.

value
Object

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

Returns

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

Attributes

Exceptions

enumType or value is null.

enumType is not an Enum.

-or-

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

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Examples

The following example illustrates the use of GetName.

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

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.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
.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

GetName<TEnum>(TEnum)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

C#
public static string? GetName<TEnum>(TEnum value) where TEnum : struct;

Type Parameters

TEnum

The type of the enumeration.

Parameters

value
TEnum

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

Returns

A string containing the name of the enumerated constant in TEnum whose value is value; or null if no such constant is found.

Exceptions

.NET 8 and later versions: TEnum is a Boolean-backed enumeration type.

Applies to

.NET 9 and other versions
Product Versions
.NET 5, 6, 7, 8, 9