Enum.GetNames Method
.NET Framework 2.0
Retrieves an array of the names of the constants in a specified enumeration.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| enumType is a null reference (Nothing in Visual Basic). |
|
| enumType parameter is not an Enum. |
The following code sample illustrates the use of GetNames :
using System; public class GetNamesTest { enum Colors { Red, Green, Blue, Yellow }; enum Styles { Plaid, Striped, Tartan, Corduroy }; public static void Main() { Console.WriteLine("The values of the Colors Enum are:"); foreach(string s in Enum.GetNames(typeof(Colors))) Console.WriteLine(s); Console.WriteLine(); Console.WriteLine("The values of the Styles Enum are:"); foreach(string s in Enum.GetNames(typeof(Styles))) Console.WriteLine(s); } }
import System.*;
public class GetNamesTest
{
enum Colors
{
red (0),
green (1),
blue (2),
yellow (3);
} //Colors
enum Styles
{
plaid (0),
striped (1),
tartan (2),
corduroy (3);
} //Styles
public static void main(String[] args)
{
Console.WriteLine("The values of the Colors Enum are:");
String s[] = Enum.GetNames(Colors.class.ToType());
for (int iCtr = 0; iCtr < s.length; iCtr++) {
Console.WriteLine(s[iCtr]);
}
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
String s1[] = Enum.GetNames(Styles.class.ToType());
for (int iCtr = 0; iCtr < s.length; iCtr++) {
Console.WriteLine(s1[iCtr]);
}
} //main
} //GetNamesTest
import System; public class GetNamesTest { enum Colors { Red, Green, Blue, Yellow }; enum Styles { Plaid, Striped, Tartan, Corduroy }; public static function Main() { Console.WriteLine("The values of the Colors Enum are:"); for(var i : int in Enum.GetNames(Colors)) Console.WriteLine(Enum.GetNames(Colors).GetValue(i)); Console.WriteLine(); Console.WriteLine("The values of the Styles Enum are:"); for(var j : int in Enum.GetNames(Styles)) Console.WriteLine(Enum.GetNames(Styles).GetValue(j)); } }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.