Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Type.IsEnumDefined() Method
Next article icon

C# Enumeration (enum)

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Enumeration (enum) is a value data type in C#. It is mainly used to assign the names or string values to integral constants, that make a program easy to read and maintain. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit.

  • Enumeration is declared using the enum keyword directly inside a namespace, class, or structure.
  • The Objective of the enum is to define our data types(Enumerated Data Types).
  • Enum can be used to define the custom integrals to the strings.
  • By default the base type of enum is int.

Declaring and Using an enum

An enum is declared using the enum keyword followed by the enumeration name and its members.

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

C#
// Using enum in C#  using System;  class Program { 	// Enum declaration 	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.

Syntax

enum Enum_variable

{

string_1…;

string_2…;

.

.

}

In above syntax, Enum_variable is the name of the enumerator, and string_1 is attached with value 0, string_2 is attached value 1 and so on. Because by default, the first member of an enum has the value 0, and the value of each successive enum member is increased by 1. We can change this default value.


Custom Value Assignment in enum

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

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

C#
// C# program to illustrate the enums // with their default values using System;  class Geeks  { // making an enumerator 'month'  enum month   {      // following are the data members     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 3: Using enum to find the shapes to find their perimeter.

C#
// C# program to illustrate the Enums 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 { 	// Main Method 	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 above example we create an enum and define the two strings which are Circle and Square by default initialized to value 0 and similarly, Square is assigned value 1 inside the class Perimeter. There is also one member function peri() which takes one parameter as a value to initializes the side/radius. Another parameter is used to judge the shape whether it is a circle or square in the form of an integer value(0 or 1). We directly use the Shapes. A circle which returns 0 and identifies the shape is equal to a circle else it is 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 4: Custom value initialization in enum.

C#
// C# program custom enum values 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); 	 	} } 

Explanation: In the above example as shown by default enum values are initialised with the 0 as in days we saw that the values are Sun(0) and Mon(1). But when we assign the B=6 in a random enum it’s next C initializes the value as 7. If we change the value of the first data member of the enum, further data members of the enums will increase by 1.

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 5: Changing the type of enum’s data member

C#
// C# program to illustrate the changing  // of data type of enum members 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.

When to Use enum?

  • When you have a set of related integral constants.
  • When you want type safety (unlike const values, which can be assigned to any int).


Next Article
C# | Type.IsEnumDefined() Method
author
alpha_786
Improve
Article Tags :
  • C#
  • CSharp-Basics
  • CSharp-data-types

Similar Reads

  • C# | Type.IsEnumDefined() Method
    Type.IsEnumDefined(Object) Method is used to return a value which indicates whether the specified value exists in the current enumeration type. Syntax: public virtual bool IsEnumDefined (object value); Here, it takes the value to be tested. Return Value: This method returns true if the specified val
    3 min read
  • Stack.GetEnumerator Method in C#
    This method returns an IEnumerator that iterates through the Stack. And it comes under the System.Collections namespace. Syntax: public virtual System.Collections.IEnumerator GetEnumerator (); Below programs illustrate the use of above-discussed method: Example 1: // C# program to illustrate the //
    2 min read
  • C# | Array.GetEnumerator Method
    This method is used to return an IEnumerator for the Array. Syntax: public System.Collections.IEnumerator GetEnumerator (); Return Value: This method returns an IEnumerator for the Array. Below programs illustrate the use of Array.GetEnumerator Method: Example 1: // C# program to demonstrate // GetE
    2 min read
  • C# | Type.GetEnumName() Method
    Type.GetEnumName(Object) Method is used to return the name of the constant which has the specified value for the current enumeration type. Syntax: public virtual string GetEnumName (object value); Here, it takes the value whose name is to be retrieved.Return Value: This method returns the name of th
    3 min read
  • C# | Type.GetEnumNames() Method
    Type.GetEnumNames() Method is used to return the names of the members of the current enumeration type. Syntax: public virtual string[] GetEnumNames (); Returns: This method returns an array which contains the names of the members of the enumeration.Exception: This method will give ArgumentException
    2 min read
  • C# | Type.GetEnumValues() Method
    Type.GetEnumValues() Method is used to return an array of the values of the constants in the current enumeration type.Syntax: public virtual Array GetEnumValues (); Return Value: This method returns an array which contains the values. The elements of the array are sorted by the binary values i.e. th
    2 min read
  • C# | CharEnumerator.MoveNext() Method
    CharEnumerator.MoveNext() Method is used to increments the internal index of the current CharEnumerator object to the next character of the enumerated string. Syntax: public bool MoveNext(); Return Value: This method returns the boolean value true value if the index is successfully incremented and w
    2 min read
  • C# | CharEnumerator.Reset() Method
    CharEnumerator.Reset Method is used to initializes the index to a position logically before the first character of the enumerated string. Syntax: public void Reset (); Below are the programs to illustrate the use of CharEnumerator.Reset() Method: Example 1: // C# program to illustrate the // use of
    2 min read
  • C# | Type.GetEnumUnderlyingType() Method
    Type.GetEnumUnderlyingType() Method is used to return the underlying type of the current enumeration type. Syntax: public virtual Type GetEnumUnderlyingType (); Return Value: This method returns the underlying type of the current enumeration. Exception: This method will return the ArgumentException
    2 min read
  • C# | Getting an enumerator for the entire ArrayList
    ArrayList.GetEnumerator Method is used to get an enumerator for the entire ArrayList. Syntax: public virtual System.Collections.IEnumerator GetEnumerator (); Return Value: It returns an IEnumerator for the entire ArrayList. Below programs illustrate the use of above-discussed method: Example 1: // C
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences