Retrieves an array of the names of the constants in a specified enumeration.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function GetNames ( _
enumType As Type _
) As String()
Dim enumType As Type
Dim returnValue As String()
returnValue = Enum.GetNames(enumType)
[ComVisibleAttribute(true)]
public static string[] GetNames(
Type enumType
)
[ComVisibleAttribute(true)]
public:
static array<String^>^ GetNames(
Type^ enumType
)
public static function GetNames(
enumType : Type
) : String[]
Return Value
Type:
array<System..::.String>[]()[]A string array of the names of the constants in enumType.
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.
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
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);
}
}
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 );
}
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 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.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference