Loop through an enum data type : System.Enum « Data Type « C# / CSharp Tutorial






using System;

enum EmployeeType : byte 
{
  Manager = 10,
  Programmer = 1,
  Contractor = 100,
  Developer = 9
}

class MainClass
{
  public static void Main(string[] args)
  {
    Array obj = Enum.GetValues(typeof(EmployeeType));
    foreach(EmployeeType e in obj)
    {
      Console.Write("String name: {0}", Enum.Format(typeof(EmployeeType), e, "G"));
      Console.Write(" ({0})", Enum.Format(typeof(EmployeeType), e, "D"));
      Console.Write(" hex: {0}\n", Enum.Format(typeof(EmployeeType), e, "X"));
      Console.WriteLine();
    }

  }
}
String name: Programmer (1) hex: 01

String name: Developer (9) hex: 09

String name: Manager (10) hex: 0A

String name: Contractor (100) hex: 64








2.41.System.Enum
2.41.1.The System.Enum Type: GetValues, GetName and GetType
2.41.2.The System.Enum Type: GetNames
2.41.3.The System.Enum Type: enum value from a string, ignore case
2.41.4.The System.Enum Type: see if a specific value is a defined enum member
2.41.5.Get underlying type of an enum: Enum.GetUnderlyingType()
2.41.6.Get an enum's type, hex and value: Enum.Format()
2.41.7.Parse a string to an enum: Enum.Parse
2.41.8.Get all stats for an enum: Enum.GetValues()
2.41.9.Loop through an enum data type
2.41.10.Enum.IsDefined()
2.41.11.Enum elements compare