Freigeben über


Enum.GetName Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: August 2009

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

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Shared Function GetName ( _
    enumType As Type, _
    value As Object _
) As String
[ComVisibleAttribute(true)]
public static string GetName(
    Type enumType,
    Object value
)

Parameters

  • 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 nulla null reference (Nothing in Visual Basic) if no such constant is found.

Exceptions

Exception Condition
ArgumentNullException

enumType or value is a nulla null reference (Nothing in Visual Basic).

ArgumentException

enumType is not an Enum.

-or-

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

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.

Examples

The following example illustrates the use of GetName.

Public Class Example
   Enum Colors
      Red
      Green
      Blue
      Yellow
   End Enum 'Colors

   Enum Styles
      Plaid
      Striped
      Tartan
      Corduroy
   End Enum 

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += String.Format("The 4th value of the Colors Enum is {0}", _
                                        [Enum].GetName(GetType(Colors), 3)) & vbCrLf
      outputBlock.Text += String.Format("The 4th value of the Styles Enum is {0}", _
                                        [Enum].GetName(GetType(Styles), 3)) & vbCrLf
   End Sub
End Class 
' The example displays the following output:
'       The 4th value of the Colors Enum is Yellow
'       The 4th value of the Styles Enum is Corduroy
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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Change History

Date

History

Reason

August 2009

Added the Remarks section.

Customer feedback.