[C#] C# Enums

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the class “Programming 2”


Enums

An enum is a special “class” that represents a group of constants (unchangeable/read-only variables).

Enum is short for “enumerations”, which means “specifically listed”.

To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma:

enum Level
{
  Low,
  Medium,
  High
}

// You can access enum items with the dot syntax:
Level myVar = Level.Medium;
Console.WriteLine(myVar);

By default, the associated constant values of enum members are of type int; they start with zero and increase by one following the definition text order. You can explicitly specify any other integral numeric type as an underlying type of an enumeration type. You can also explicitly specify the associated constant values, as the following example shows:

enum ErrorCode:ushort
{
  None = 0,
  Unknown = 1,
  ConnectionLost = 100,
  OutlierReading = 200
}
using System;

enum Planet
{
  mercury,
  venus,
  earth,
}

class Program
{
  static void Main()
  {
    // Type casting enum to int
    int Planet1 = (int)Planet.mercury;
    int Planet2 = (int)Planet.venus;
    int Planet3 = (int)Planet.earth;

    Console.WriteLine("Value of first member: " + planet1);
    Console.WriteLine("Value of second member: " + planet2);
    Console.WriteLine("Value of third member: " + planet3);
  }
}


Enum inside a Class

You can also have an enum inside a class:

class Program
{
  enum Level
  {
    Low,
    Medium,
    High
  }

  static void Main(string[] args)
  {
    Level myVar = Level.Medium;
    Console.WriteLine(myVar);
  }
}

// Output: Medium


Enum Values

By default, the first item of an enum has the value 0. The second has the value 1, and so on.

To get the integer value from an item, you must explicitly convert the item to an int:

enum Months
{
  January, // 0
  February, // 1
  March, // 2
  April, // 3
  May, // 4
  June, // 5
  July // 6
}

static void Main(string[] args)
{
  int myNum = (int) Months.April;
  Console.WriteLine(myNum);
}

// Output: 3

You can also assign your own enum values, and the next items will update their numbers accordingly:

enum Months
{
  January, // 0
  February, // 1
  March=6, // 6
  April, // 7
  May, // 8
  June, // 9
  July // 10
}

static void Main(string[] args)
{
  int myNum = (int) Months.April;
  Console.WriteLine(myNum);
}

// Output: 7


Enum in a Switch Statement

Enums are often used in switch statements to check for corresponding values:

enum Level
{
  Low,
  Medium,
  High
}

static void Main(string[] args)
{
  Level myVar = Level.Medium;

  switch(myVar)
  {
    case Level.Low:
      Console.WriteLine("Low level");
      break;
    case Level.Medium:
      Console.WriteLine("Medium level");
      break;
    case Level.High:
      Console.WriteLine("High level");
      break;
  }
}

// Output: Medium level

Why And When To Use Enums?

Use enums when you have values that you know aren’t going to change, like month days, days, colors, deck of cards, etc.




Back to Top

See other articles in Category CS

Leave a comment