Enumeration (enum) in C#

Last Updated : 11 Sep, 2025

Enumeration (enum) in C# is a special value type that defines a set of named constants. It improves code readability by giving meaningful names to numeric values.

Example: Enum of Days in a week

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

  • Value type: Stored as integers by default (can use other integral types like byte, short, long).
  • Default underlying type: int (starting from 0 unless specified).
  • Custom values: You can assign explicit numeric values to members.
  • Strongly typed constants: Prevents use of invalid values.
  • Improves readability: Use enum Days.Monday instead of 0.
  • Useful in switch statements for handling predefined options.
  • Scoped to their type: Prevents accidental mixing of unrelated constants.

Declaring and Using an enum

Enumeration is declared using the enum keyword directly inside a namespace, class or structure.

Example: Using Enumeration within the same class to store the book with their index.

C#
using System;

class Program{
    
	enum Books{cSharp, Javascript, Kotlin, Python, Java}
	
	public static void Main(string[] args){
       // Get the value of the enum
		Console.WriteLine("Book: Java at Index:"+  (int)Books.Java);
		Console.WriteLine("Book: Python at Index:"+  (int)Books.Python);
		Console.WriteLine("Book: Kotlin at Index:"+ (int) Books.Kotlin);
		Console.WriteLine("Book: Javascript at Index:"+  (int)Books.Javascript);
		Console.WriteLine("Book: cSharp at Index:"+  (int)Books.cSharp);
	}
}

Output
Book: Java at Index:4
Book: Python at Index:3
Book: Kotlin at Index:2
Book: Javascript at Index:1
Book: cSharp at Index:0

Explanation: In the above example we create a enum (Enumeration) to declare the name of books and access their index value which is starting from the 0 in the enum and type cast the value to get the values in numeric from.

Custom Value Assignment in enum

By default, the first member starts at 0, but you can assign custom values.

Example: Another example of an enum is to show the month name with its default value.

C#
using System;

class Geeks {

    enum month { jan, feb, mar, apr, may }
    static void Main(string[] args)
    {
        // getting the integer values of data members..
        Console.WriteLine("The value of jan in month " + "enum is " + (int)month.jan);
        
        Console.WriteLine("The value of feb in month " + "enum is " + (int)month.feb);
        
        Console.WriteLine("The value of mar in month " + "enum is " + (int)month.mar);
        
        Console.WriteLine("The value of apr in month " + "enum is " + (int)month.apr);
        
        Console.WriteLine("The value of may in month " + "enum is " + (int)month.may);
    }
}

Output
The value of jan in month enum is 0
The value of feb in month enum is 1
The value of mar in month enum is 2
The value of apr in month enum is 3
The value of may in month enum is 4

Explanation: In the above code we create a enum with month name, its data members are the name of months like jan, feb, mar, apr, may. Then print the default integer values of these enums. An explicit cast is required to convert from enum type to an integral type.

Using enum in Conditional Statements

Example: Using enum to find the shapes to find their perimeter.

C#
using System;

class Perimeter {
    // declaring enum
    public enum Shapes { Circle, Square }

    public void Peri(int val, Shapes shape)
    {
        if (shape == Shapes.Circle) {
            // Output the circumference
            Console.WriteLine( "Circumference of the circle is " + 2 * 3.14 * val);
        }
        else {
            // Output the perimeter of the square
            Console.WriteLine("Perimeter of the square is "+ 4 * val);
        }
    }
}

class Geeks {
    static void Main(string[] args)
    {
        Perimeter o = new Perimeter();

        // Display the perimeter of the circle
        o.Peri(3, Perimeter.Shapes.Circle);

        // Display the perimeter of the square
        o.Peri(4, Perimeter.Shapes.Square);
    }
}

Explanation: In the example, an enum Shapes is defined with Circle = 0 and Square = 1. The class Perimeter has a method peri() that takes one parameter for side/radius and another (0 or 1) to identify the shape using Shapes.Circle or Shapes.Square. If the value is 0, it’s a circle; otherwise, it’s a square.

Custom Value Initialization in enum

The default value of first enum member is set to 0 and it increases by 1 for the further data members of enum. However, the user can also change these default value.

enum days {

day1 = 1,
day2 = day1 + 1,
day3 = day1 + 2
.
.

}

Changing the Data Type of enum

By default, enum values are of type int, but you can change them to byte, short, long, etc.

Example: Custom value initialization in enum.

C#
using System;

class Geeks{
   // Enum declaration
   
	enum days { Sun, Mon, tue, Wed };
	
	enum random { A, B=6, C, D};
	
	static void Main(string[] args){
		// days enum values are started 0 by default
		Console.WriteLine("Sun = " + (int)days.Sun);
		Console.WriteLine("Mon = " + (int)days.Mon);

        // The value started from 0 by default
		Console.WriteLine("A = " + (int)random.A);

		// Explicitly assigning values: B is assigned 6, so C automatically gets the next value, which is 7
		Console.WriteLine("B = " + (int)random.B);

		// The value become 7 as C is next to B
		Console.WriteLine("C = " + (int)random.C);
	}
}

Note: Now, if the data member of the enum member has not been initialized, then its value is set according to the rules stated below: 

  • If it is the first member, then its value is set to 0 otherwise.
  • It sets out the value which is obtained by adding 1 to the previous value of the f enum data member.

Example: Changing the type of enum’s data member

C#
using System;

// changing the type to byte using :
enum Button : byte {
	
	// OFF denotes the Button is switched Off... with value 0
	OFF,
	// ON denotes the Button is switched on.. with value 1
	ON
}

class Geeks
 {
	static void Main(string[] args)
	{
		// Declaring a variable of type byte and assigning it the value of ON
		byte b = 1;

		if (b == (byte)Button.OFF){
			Console.WriteLine("The electric switch is Off");
		}
		
		else if (b == (byte)Button.ON) {
			Console.WriteLine("The electric switch is ON");
		}
		
		else{
			Console.WriteLine("byte cannot hold such" + " large value");
		}
	}
}

Output
The electric switch is ON

Explanation: By default the base data type of enumerator in C# is int. However, the user can change it as per convenience like byte, byte, short, short, int, uint, long, etc In this example, we change the data type as byte.

Comment

Explore