Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Enum.GetNames Method

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

[Visual Basic]
Public Shared Function GetNames( _
   ByVal enumType As Type _
) As String()
[C#]
public static string[] GetNames(
 Type enumType
);
[C++]
public: static String* GetNames(
 Type* enumType
)  __gc[];
[JScript]
public static function GetNames(
   enumType : Type
) : String[];

Parameters

enumType
An enumeration type.

Return Value

A string array of the names of the constants in enumType. The elements of the array are sorted by the values of the enumerated constants.

Exceptions

Exception Type Condition
ArgumentNullException enumType is a null reference (Nothing in Visual Basic).
ArgumentException enumType parameter is not an Enum.

Example

The following code sample illustrates the use of GetNames:

[Visual Basic] 
Imports System

Public Class GetNamesTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 'Colors
    
    Enum Styles
        Plaid
        Striped
        Tartan
        Corduroy
    End Enum 'Styles
    
    Public Shared Sub Main()
        
        Console.WriteLine("The values of the Colors Enum are:")
        Dim s As String
        For Each s In  [Enum].GetNames(GetType(Colors))
            Console.WriteLine(s)
        Next s

        Console.WriteLine()
        
        Console.WriteLine("The values of the Styles Enum are:")

        For Each s In  [Enum].GetNames(GetType(Styles))
            Console.WriteLine(s)
        Next s
    End Sub 'Main
End Class 'GetNamesTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

__value enum Colors { Red, Green, Blue, Yellow };
__value enum Styles { Plaid, Striped, Tartan, Corduroy };

int main() {

    Console::WriteLine("The values of the Colors Enum are:");
    Array* a = Enum::GetNames(__typeof(Colors));
    Int32 i = 0;
    do{
        Object* o = a->GetValue(i);
        Console::WriteLine(o->ToString());
    }while ( ++i < a->Length );
    Console::WriteLine();

    Console::WriteLine("The values of the Styles Enum are:");
    Array* b = Enum::GetNames(__typeof(Styles));
    i = 0;
    do{
        Object* o = b->GetValue(i);
        Console::WriteLine(o->ToString());
    }while( ++i < b->Length );
}

[JScript] 
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));
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Enum Class | Enum Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.