Get an array of the names of the constants in a specified enum in CSharp

Description

The following code shows how to get an array of the names of the constants in a specified enum.

Example


// w  ww  . java2s  .co  m
using System;
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 »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var