Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
GetNames(Type) |
Retrieves an array of the names of the constants in a specified enumeration. |
GetNames<TEnum>() |
Retrieves an array of the names of the constants in a specified enumeration type. |
Retrieves an array of the names of the constants in a specified enumeration.
public:
static cli::array <System::String ^> ^ GetNames(Type ^ enumType);
public static string[] GetNames(Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static string[] GetNames(Type enumType);
static member GetNames : Type -> string[]
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetNames : Type -> string[]
Public Shared Function GetNames (enumType As Type) As String()
An enumeration type.
A string array of the names of the constants in enumType
.
enumType
is null
.
enumType
parameter is not an Enum.
.NET 8 and later versions: enumType
is a Boolean-backed enumeration type.
The following example illustrates the use of the GetNames method.
using System;
public class GetNamesTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The members of the Colors enum are:");
foreach(string s in Enum.GetNames(typeof(Colors)))
Console.WriteLine(s);
Console.WriteLine();
Console.WriteLine("The members of the Styles enum are:");
foreach(string s in Enum.GetNames(typeof(Styles)))
Console.WriteLine(s);
}
}
// The example displays the following output:
// The members of the Colors enum are:
// Red
// Green
// Blue
// Yellow
//
// The members of the Styles enum are:
// Plaid
// Striped
// Tartan
// Corduroy
open System
type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3
type Styles =
| Plaid = 0
| Striped = 1
| Tartan = 2
| Corduroy = 3
printfn "The members of the Colors enum are:"
for s in Enum.GetNames typeof<Colors> do
printfn $"{s}"
printfn "\nThe members of the Styles enum are:"
for s in Enum.GetNames typeof<Styles> do
printfn $"{s}"
// The example displays the following output:
// The members of the Colors enum are:
// Red
// Green
// Blue
// Yellow
//
// The members of the Styles enum are:
// Plaid
// Striped
// Tartan
// Corduroy
Public Class GetNamesTest
Enum Colors
Red
Green
Blue
Yellow
End Enum
Enum Styles
Plaid
Striped
Tartan
Corduroy
End Enum
Public Shared Sub Main()
Console.WriteLine("The members of the Colors enum are:")
For Each s In [Enum].GetNames(GetType(Colors))
Console.WriteLine(s)
Next
Console.WriteLine()
Console.WriteLine("The members of the Styles enum are:")
For Each s In [Enum].GetNames(GetType(Styles))
Console.WriteLine(s)
Next
End Sub
End Class
' The example displays the following output:
' The members of the Colors enum are:
' Red
' Green
' Blue
' Yellow
'
' The members of the Styles enum are:
' Plaid
' Striped
' Tartan
' Corduroy
The elements of the return value array are sorted by the binary values of the enumerated constants (that is, by their unsigned magnitude). The following example provides displays information about the array returned by the GetNames method for an enumeration that includes a negative, zero, and a positive value.
using System;
enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };
public class Example
{
public static void Main()
{
foreach (var name in Enum.GetNames(typeof(SignMagnitude))) {
Console.WriteLine("{0,3:D} 0x{0:X} {1}",
Enum.Parse(typeof(SignMagnitude), name),
name);
} }
}
// The example displays the following output:
// 0 0x00000000 Zero
// 1 0x00000001 Positive
// -1 0xFFFFFFFF Negative
open System
type SignMagnitude =
| Negative = -1
| Zero = 0
| Positive = 1
for name in Enum.GetNames typeof<SignMagnitude> do
let p = Enum.Parse(typeof<SignMagnitude>, name)
printfn $"{p,3:D} 0x{p:X} {name}"
// The example displays the following output:
// 0 0x00000000 Zero
// 1 0x00000001 Positive
// -1 0xFFFFFFFF Negative
Public Enum SignMagnitude As Integer
Negative = -1
Zero = 0
Positive = 1
End Enum
Module Example
Public Sub Main()
Dim names() As String = [Enum].GetNames(GetType(SignMagnitude))
For Each name In names
Console.WriteLine("{0,3:D} 0x{0:X} {1}",
[Enum].Parse(GetType(SignMagnitude), name),
name)
Next
End Sub
End Module
' The example displays the following output:
' 0 0x00000000 Zero
' 1 0x00000001 Positive
' -1 0xFFFFFFFF Negative
If there are enumerated constants with same value, the order of their corresponding names is unspecified.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Retrieves an array of the names of the constants in a specified enumeration type.
public:
generic <typename TEnum>
where TEnum : value class static cli::array <System::String ^> ^ GetNames();
public static string[] GetNames<TEnum>() where TEnum : struct;
static member GetNames : unit -> string[] (requires 'Enum : struct)
Public Shared Function GetNames(Of TEnum As Structure) () As String()
The type of the enumeration.
A string array of the names of the constants in TEnum
.
.NET 8 and later versions: TEnum
is a Boolean-backed enumeration type.
Product | Versions |
---|---|
.NET | 5, 6, 7, 8, 9, 10 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in