C# Enum

Summary: in this tutorial, you will learn about the C# enum type and how to use it effectively.

Introduction to the C# enum type

An enum type (or enumeration type) is a value type that allows you to define a group of named numeric constants.

To define an enum type, you use the enum keyword and specify the names of the enum members. For example:

enum Status
{
    open,
    inProgress,
    resolved,
    closed,
    reopened
};Code language: C# (cs)

In this example, we define an enum type called Status. The Status enum has five members open, inProgress, resolved, closed, and reopened.

The following example shows how to use the Status enum:

namespace CSharpTutorial;

class Program
{
    enum Status { 
        open, 
        inProgress, 
        resolved, 
        closed, 
        reopened 
    };

    static void Main(string[] args)
    {
        Status status = Status.open;
        bool isOpen = status == Status.open;

        Console.WriteLine(isOpen); // True
    }
}Code language: C# (cs)

In this example:

  • First, define a variable with the Status enum type and initialize its values to the Status.open member.
  • Second, compare the status with the Status.open member and assign the result to the isOpen variable. The isOpen is true as expected.

Note that to reference an enum member, you use the enum name, dot operator, and member name.

The members of an enum type have the following characteristics:

  • Have the values of type int
  • Have the default values of 0, 1, 2, …

For example, in the Status enum, the types of the enum members are integers (int). The open member takes a value of 0, the inProgress take the value of 1, etc.

C# allows you to specify an alternative integral type for the enum members. For example:

enum Status : byte {
    open, 
    inProgress, 
    resolved, 
    closed,
    reopened 
};Code language: C# (cs)

In this example, instead of using the default int type, the Status enum uses the byte type. C# also allows you to specify an explicit value for each enum member. For example:

enum Status 
{
    open = 1,
    inProgress = 2,
    resolved = 3,
    closed = 4,
    reopened = 5
};Code language: C# (cs)

If you use explicit values for some members, the unassigned members keep incrementing from the last explicit value. For example:

enum Status 
{
    open = 10,
    inProgress, // 11
    resolved, // 12
    closed = 20,
    reopened = 30
};Code language: C# (cs)

In this example, the inProgress and resolved members will have the values of 11 and 12 respectively.

Convert enum to int

C# allows you to convert an enum instance to an integer. For example:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {
        Status status = Status.inProgress;
        int statusValue = (int)status;

        Console.WriteLine(statusValue); // 1
    }
}Code language: C# (cs)

In this example, we explicitly cast the instance of the Status enum to an integer. As the result, the statusValue is 1, which is the underlying value of the Status.inProgress.

The following example illustrates how to convert an integer to an instance of an enum:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {
        int statusValue = 0;
        Status status = (Status)statusValue;

        Console.WriteLine(status == Status.open); // true
    }
}Code language: C# (cs)

Convert enum to string

To convert an instance of an enum type to a string, you use the toString() method. For example:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {
        var status = Status.open;
        Console.WriteLine(status.ToString()); // open
    }
}Code language: C# (cs)

In this example, we convert the status enum instance to the string, which returns “open” as expected.

The following example shows how to convert a string into an enum instance:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {
        Enum.TryParse("open", out Status status);

        Console.WriteLine(status == Status.open); // true

    }
}Code language: C# (cs)

Iterate enum members

To get all the values of the enum members, you use the Enum.GetValues() method. For example:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {

        foreach (int value in Enum.GetValues(typeof(Status)))
        {
            Console.WriteLine(value);
        }

    }
}Code language: C# (cs)

Output:

0
1
2
3
4Code language: C# (cs)

To get the names of enum members, you use the Enum.GetNames() method. For example:

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };

    static void Main(string[] args)
    {

        foreach (string name in Enum.GetNames(typeof(Status)))
        {
            Console.WriteLine(name);
        }

    }
}Code language: C# (cs)

Output:

open
inProgress
resolved
closed
reopenedCode language: C# (cs)

C# enum in a switch statement

The following program reads an integer from the console and prints out the corresponding enum member using the switch statement.

namespace CSharpTutorial;

class Program
{
    enum Status
    {
        open,
        inProgress,
        resolved,
        closed,
        reopened
    };
    static void Main(string[] args)
    {
        Console.Write("Enter a status (0-4):");
        var statusValue = Convert.ToInt32(Console.ReadLine());
        Status status = (Status)statusValue;

        switch (status)
        {
            case Status.open:
                Console.WriteLine("The status is open");
                break;
            case Status.inProgress:
                Console.WriteLine("The status is in progress");
                break;
            case Status.resolved:
                Console.WriteLine("The status is resolved");
                break;
            case Status.closed:
                Console.WriteLine("The status is closed");
                break;
            case Status.reopened:
                Console.WriteLine("The status is reopened");
                break;
        }

    }
}Code language: C# (cs)

Summary

  • An enum type is a value type that defines a group of named numeric constants.
  • Enum members have the type int and default values of 0, 1, 2…
Was this tutorial helpful ?