Enum keyword


In this article we are going to discuss about enum keyword with examples in C#.

Enum keyword in C#

The enum keyword in C# is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Enums are typically used to define a collection of related values that can be assigned to a variable, improving code readability and maintainability.

Usage of enum

An enum is defined using the enum keyword followed by the name of the enumeration and a list of named constants.

using System;
public enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}
public class Program
{
    public static void Main()
    {
        DaysOfWeek today = DaysOfWeek.Monday;

        if (today == DaysOfWeek.Monday)
        {
            Console.WriteLine("Start of the work week!");
        }
    }
}

In the above example, DaysOfWeek is an enumeration with the days of the week as its named constants.

Output

Enum Keyword in C#

Is it possible to inherit Enum in C#?

No, it is not possible to inherit enums in C#. Enums in C# are a value type and are derived from the System.Enum class. They cannot be extended or inherited like classes. Enums are sealed by design to provide a fixed set of named constants.

Enum Keyword in C#
Prev Next