EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

Enum.GetName Method

May 11, 2014

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

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

public static string GetName(
	Type enumType,
	Object value
)

Parameters

enumType
Type: System.Type
An enumeration type.
value
Type: System.Object
The value of the underlying type of a particular enumerated constant.

Return Value

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

ExceptionCondition
ArgumentNullException

enumType or value is a 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 Example
{
   enum Colors { Red, Green, Blue, Yellow };
   enum Styles { Plaid, Striped, Tartan, Corduroy };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      outputBlock.Text += String.Format("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3)) + "\n";
      outputBlock.Text += String.Format("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3)) + "\n";
   }
}
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft