Mono Class Library: System.Enum Overview | Members

System.Enum.GetName Method

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

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

Parameters

enumType
A Type that specifies the type of the enumeration.
value
A object that contains the integral value or the name of an enumeration constant.

Returns

A string containing the name of the enumerated constant in enumType whose value is value, or a null reference if no such constant is found. If multiple constants have the same value, as to which name is returned for that value, is unspecified.

Exceptions

TypeReason
ArgumentNullExceptionenumType or value is a null reference.
ArgumentException

enumType is not a Type that describes a Enum .

-or-

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

Remarks

Documentation for this section has not yet been entered.

Example

The following example demonstrates the Enum.GetName(Type, object) method.

C# Example

using System;
public enum Signs {
  Stop = 1,
  Yield = 2,
  Merge = 4
};
public class EnumCompTo {
  public static void Main() {
   Console.Write( "The name of the constant with the value 4 is: " );
   Console.WriteLine( "{0}.", Signs.GetName(typeof(Signs), 4));
   Console.Write( "The name of the constant with the value Stop is: " );
   Console.WriteLine( "{0}.", Signs.GetName(typeof(Signs), Signs.Stop ));
  }
}

The output is

The name of the constant with the value 4 is: Merge.
The name of the constant with the value Stop is: Stop.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0