Enum.GetNames Method

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

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

[ComVisibleAttribute(true)]
public static string[] GetNames(
	Type enumType
)

Parameters

enumType
Type: System.Type
An enumeration type.

Return Value

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

ExceptionCondition
ArgumentNullException

enumType is null.

ArgumentException

enumType is not an Enum.

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.

The following example illustrates the use of the GetNames method.


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";
   }
}


Silverlight

Supported in: 5

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

Community Additions

ADD
Show: