.NET Framework Class Library
Enum..::.GetNames Method

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

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

Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function GetNames ( _
    enumType As Type _
) As String()
Visual Basic (Usage)
Dim enumType As Type
Dim returnValue As String()

returnValue = Enum.GetNames(enumType)
C#
[ComVisibleAttribute(true)]
public static string[] GetNames(
    Type enumType
)
Visual C++
[ComVisibleAttribute(true)]
public:
static array<String^>^ GetNames(
    Type^ enumType
)
JScript
public static function GetNames(
    enumType : Type
) : String[]

Parameters

enumType
Type: System..::.Type
An enumeration type.

Return Value

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

ExceptionCondition
ArgumentNullException

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

ArgumentException

enumType parameter is not an Enum.

Remarks

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.

Examples

The following example 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);
    }
}
Visual C++
using namespace System;
enum class Colors
{
   Red, Green, Blue, Yellow
};

enum class Styles
{
   Plaid, Striped, Tartan, Corduroy
};

int main()
{
   Console::WriteLine( "The values of the Colors Enum are:" );
   Array^ a = Enum::GetNames( Colors::typeid );
   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( Styles::typeid );
   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));
    }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker