C# Enums (enumerations, named constants) Tutorial

In this tutorial we learn how enums allow us to use words instead of numbers (like true and false). We cover how to declare an enum, how to define our own custom values for it and how to access it.

What is an enum

An enum, or enumeration, is a set of named integer constants. They allow us to create a set of symbolic names that map to known numerical values.

The term enum should not be confused with enumerator. An enum is a custom data type of name/value pairs while an enumerator is a class or struct that implements an IEnumerable.

The easiest way to understand an enum is to use a bool as an example.

A boolean allows us to write the words true or false instead of the numbers 1 or 0. An enum is a custom variation where we declare our own words to map to numbers.

How to declare an enum

We declare an enum by writing the keyword enum, followed by a name and a code block. Inside the code block we specify our name/value pairs of enums, separated by a comma.

An enum’s code block must be terminated with a semicolon.

Syntax:
enum identifier
{
    // list of enums
};
Example:
namespace Enums
{
    class Program
    {
        static void Main()
        { }

        enum DoW
        {
            Mon,
            Tue,
            Wed,
            Thu,
            Fri,
            Sat,
            Sun
        };
    }
}

In the example above we declared an enum with names for each day of the week. These names represent integers.

Example:
enum DoW
{
    Mon, // 0
    Tue, // 1
    Wed, // 2
    Thu, // 3
    Fri, // 4
    Sat, // 5
    Sun  // 6
}

Each name will represent an integer in our code, like true represents 1 in a boolean.

How to define an enum with custom values

We don’t have to use the default numbers for an enum. We can declare an enum with our own custom integer values.

To define our custom integer value, we use the assignment operator.

Syntax:
type identifier
{
    enumName = intValue
}
Example:
namespace Enums
{
    class Program
    {
        static void Main() { }

        enum DoW
        {
            Mon = 1,
            Tue = 2,
            Wed = 3,
            Thu = 4,
            Fri = 5,
            Sat = 6,
            Sun = 7
        };
    }
}

In the example above we use custom numbers to represent our days of the week.

How to access an enum

To access the value in an enum we use its identifier and the enum element identifier with dot notation.

Syntax:
enumIdentifier.element;
Example:
using System;

namespace Enums
{
    class Program
    {
        static void Main(string[] args)
        {
            var playerInitAnim = PSM.idle;

            Console.WriteLine("Player default animation: {0} ({1})", playerInitAnim, (int)playerInitAnim);
            Console.ReadLine();
        }

        enum PSM
        {
            idle,
            walking,
            running,
            crouching,
            sneaking,
            jumping,
            falling
        };
    }
}

In the example above we set the our variable playerInitAnim to the value of the “idle” element in the PSM enum.

Enums are value data types. They contain their own values and can’t inherit or pass inheritance.

Summary: Points to remember

  • An enum is a set of named constants similar to a boolean and allows us to use words instead of numbers.
  • An enum is an enumeration, not to be confused with an enumerator.
  • Enums are value data types, stored on the stack. They cannot inherit or pass inheritance.