Bagikan melalui


Enum.GetNames Method

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

Retrieves an array of the names of the constants in a specified enumeration.

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

Syntax

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

Parameters

Return Value

Type: array<System.String[]
A string array of the names of the constants in enumType.

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentException

enumType is not an Enum.

Remarks

The elements of the return value array are sorted by the binary values of the enumerated constants (that is, by their unsigned magnitude). If there are enumerated constants with the same value, the order of their corresponding names is unspecified.

Examples

The following example illustrates the use of the GetNames method.


Public Class Example

   Enum Colors
      Red
      Green
      Blue
      Yellow
   End Enum 'Colors

   Enum Styles
      Plaid
      Striped
      Tartan
      Corduroy
   End Enum 'Styles

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= "The values of the Colors Enum are:" & vbCrLf
      Dim s As String
      For Each s In [Enum].GetNames(GetType(Colors))
         outputBlock.Text &= s & vbCrLf
      Next s

      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "The values of the Styles Enum are:" & vbCrLf

      For Each s In [Enum].GetNames(GetType(Styles))
         outputBlock.Text &= s & vbCrLf
      Next s
   End Sub 'Main
End Class 'GetNamesTest
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 += "The values of the Colors Enum are:" + "\n";
      foreach (string s in Enum.GetNames(typeof(Colors)))
         outputBlock.Text += s + "\n";

      outputBlock.Text += "\n";

      outputBlock.Text += "The values of the Styles Enum are:" + "\n";
      foreach (string s in Enum.GetNames(typeof(Styles)))
         outputBlock.Text += s + "\n";
   }
}

Version Information

Silverlight

Supported in: 5

Platforms

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

See Also

Reference