C# Enum GetName

Description

Enum GetName retrieves the name of the constant in the specified enumeration that has the specified value.

Syntax

Enum.GetName has the following syntax.


[ComVisibleAttribute(true)]/*w ww  .ja va  2  s. c om*/
public static string GetName(
  Type enumType,
  Object value
)

Parameters

Enum.GetName has the following parameters.

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

Returns

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

Example

The following example illustrates the use of GetName.


using System;// w  w w .j  av  a  2s . c om

public class GetNameTest {
    enum Colors { Red, Green, Blue, Yellow };

    public static void Main() {
        Console.WriteLine(Enum.GetName(typeof(Colors), 3));

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version