C# Enum GetNames

Description

Enum GetNames retrieves an array of the names of the constants in a specified enumeration.

Syntax

Enum.GetNames has the following syntax.


[ComVisibleAttribute(true)]
public static string[] GetNames(
  Type enumType
)

Parameters

Enum.GetNames has the following parameters.

  • enumType - An enumeration type.

Returns

Enum.GetNames method returns

Example

The following example provides displays information about the array returned by the GetNames method for an enumeration that includes a negative, zero, and positive value.


using System;/*w ww  . jav  a  2s .  com*/
enum Styles { Plaid, Striped, Tartan, Corduroy };
enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };
enum Colors { Red, Green, Blue, Yellow };
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);

        Console.WriteLine("The members of the Colors enum are:");
        foreach (string s in Enum.GetNames(typeof(Colors)))
            Console.WriteLine(s);


        Console.WriteLine("The members of the Styles enum are:");
        foreach (string s in Enum.GetNames(typeof(Styles)))
            Console.WriteLine(s);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version