C# Enums

Description

An enum is a special value type that lets you specify a group of named numeric constants.

Syntax

The keyword enum declares an enumerated type. The general form for an enumeration is


enum name { 
    enumeration list 
};

name specifies the enumeration type name. enumeration list is a comma-separated list of identifiers.

Each of the symbols stands for an integer value. Each of the symbols has a value one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0.

Example

Example for create an enum type


using System;//w  w  w.j ava2 s .co  m

enum DaysOfWeek
{
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

class MainClass
{
  static void Main(string[] args)
  {
    DaysOfWeek Today = DaysOfWeek.Monday;

    Console.WriteLine(Today);
  }
}

The code above generates the following result.

Example 2

Print out the details of any enum


using System;//from w  ww  .  jav  a 2  s .  c  om
using System.Collections.Generic;
using System.Text;

enum EmpType : byte
{
  Manager = 10,
  Grunt = 1,
  Contractor = 100,
  VicePresident = 9
}

class Program
{
  static void Main(string[] args)
  {
    EmpType e2 = EmpType.Contractor;
    DayOfWeek day = DayOfWeek.Friday;
    ConsoleColor cc = ConsoleColor.Black;
    EvaluateEnum(e2);
    EvaluateEnum(day);
    EvaluateEnum(cc);
  }
  static void EvaluateEnum(System.Enum e)
  {
    Console.WriteLine("=> Information about {0}", e.GetType().Name);
    Console.WriteLine("Underlying storage type: {0}",Enum.GetUnderlyingType(e.GetType()));
    Array enumData = Enum.GetValues(e.GetType());
    Console.WriteLine("This enum has {0} members.", enumData.Length);
    for (int i = 0; i < enumData.Length; i++)
    {
      Console.WriteLine("Name: {0}, Value: {0:D}", enumData.GetValue(i));
    }
  }
}

The code above generates the following result.

Example 3

We can create enum type from existing enum type.


enum WeekEnd{/*  w  ww.j av a2 s . c o m*/
  Saturday, Sunday
}

enum WeekDay{
   Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = WeekEnd.Saturday, Sunday = WeekEnd.Sunday
}

Example 4

Enum has a static method we can use to check if an enum value is defined.


using System;/*from  w w w .  j a va  2 s.  c  om*/

public enum WeekDay{

   Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}

class Test
{
    static void Main()
    {
        WeekDay day = (WeekDay)111;

        if (Enum.IsDefined(day.GetType(),day))
        {
            Console.WriteLine("defined");
        }
        else
        {
            Console.WriteLine("not defined");

        }

    }
}

The output:

Example 5

How to use enum with switch statement


using System;// www .  j  a v  a  2s  .  com

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

class MainClass
{
  public static void myFunction(EmployeeType e)
  {
    switch(e)
    {
      case EmployeeType.Contractor:
        Console.WriteLine("EmployeeType.Contractor");
        break;
      case EmployeeType.Programmer:
        Console.WriteLine("EmployeeType.Programmer");
        break;
      case EmployeeType.Manager:
        Console.WriteLine("EmployeeType.Manager");
        break;
      case EmployeeType.Developer:
        Console.WriteLine("EmployeeType.Developer");
        break;
      default: break;
    }
  }
  public static void Main(string[] args)
  {
    
    EmployeeType fred;
    fred = EmployeeType.Developer;
    myFunction(fred);

  }
}

The code above generates the following result.

Example 6

How to get all names for an enum


using System;//  w w  w . j  av  a2  s  .  c o  m

enum Color
{
    red,
    green,
    yellow
}

public class MainClass
{
    public static void Main()
    {
        Color c = Color.red;
        
        foreach (string s in Enum.GetNames(c.GetType()))
        {
            Console.WriteLine("Name: {0}", s);
        }
        
    }
}

The code above generates the following result.

Example 7

How to create enum from string


using System;//from w  w  w  .j av a  2 s .  com

enum Color
{
    red,
    green,
    yellow
}

public class MainClass
{
    public static void Main()
    {
        Color c = Color.red;
        
        // 
        c = (Color) Enum.Parse(typeof(Color), "Red", true);
        Console.WriteLine("string value is: {0}", c);
        
    }
}

The code above generates the following result.

Example 8

How to check if a string value is defined


using System;/*www .jav  a 2 s  .  c  om*/

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

class MainClass
{
  public static void Main(string[] args)
  {
    if(Enum.IsDefined(typeof(EmployeeType), "SalesPerson"))
      Console.WriteLine("Yep, we have sales people.");
    else
      Console.WriteLine("No, we have no profits....");
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor