Export (0) Print
Expand All

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
)
/** @attribute ComVisibleAttribute(true) */ 
public static String[] GetNames (
	Type enumType
)
ComVisibleAttribute(true) 
public static function GetNames (
	enumType : Type
) : String[]
Not applicable.

Parameters

enumType

An enumeration type.

Return Value

A string array of the names of the constants in enumType.

Exception typeCondition

ArgumentNullException

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

ArgumentException

enumType parameter is not an Enum.

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

The following example 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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft