Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Enum Values
3.
Access an Enum
4.
Get integer values from enum
5.
Enum Rules
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Enums in C#

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will learn about Enums in C#. Enum(also called enumeration) in C#  is a user-defined value type representing a list of named integer constants. It improves the program's readability maintainability and reduces complexity.

Syntax

enum_name is the name that we want to give to our enum list. We use comma (,) to separate the items in our enum list.

enum enum_name {

   enumeration list

};

Example

enum months
{
    // items of the enum
    January,   
    February, 
    March,     
    April,       
    May,       
    June,
    July      
}

Recommended topics, Palindrome in C# and  Ienumerable vs Iqueryable

Enum Values

Default values

If we do not give values to the enum items, the items are provided an integer value by the compiler by default. The compiler will assign values starting from 0, and it increments the value by one each time we add an item to our enum.

enum months
{
    // items of the enum
    January,   //0
    February, //1
    March,     //2
    April,       //3
    May,       //4
    June      //5
    July       //6
}

Assigning our values

We can change the values given to the enum item. If we change the value assigned to one of our given months, e.g. March is set to 10. Then the compiler will assign  value sequentially to the other months, i.e., it will increment by one from 10:

enum months
{
    // items of the enum
    January,          //0
    February,        //1
    March=10,      //10
    April,               //11
    May,               //12
    June,             //13
    July                //14
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Access an Enum

We can access an enum item using the dot syntax:

Example

using System;
namespace Example {
enum months
{
    // items of the enum
    January,
    February,
    March,
    April,
    May,
    June,
    July
}
public class Program {
    public static void Main(string[] args)
    {
        Console.WriteLine( months.January);
        Console.WriteLine( months.February);
        Console.WriteLine( months.March);
        Console.WriteLine( months.April);
        Console.WriteLine(months.May);
        Console.WriteLine( months.June);
        Console.WriteLine( months.July);
    }
}
}

Output

January,

February

March

April

May

June

July

Get integer values from enum

We have to explicitly convert an item in the enum list to get the integer value associated with that item.

Example

In this example, we will see how to convert the items to an integral value.

using System;
namespace Example {
enum months
{
    // items of the enum
    January,
    February,
    March,
    April,
    May,
    June,
July
}
public class Program{

    public static void Main(string[] args)
    {
        // Prints the integer values of the items
        Console.WriteLine("The value of January in months " + "enum is " + (int)months.January);
        Console.WriteLine("The value of February in months " + "enum is " + (int)months.February);
        Console.WriteLine("The value of March in months" + "enum is " + (int)months.March);
        Console.WriteLine("The value of April in months " + "enum is " + (int)months.April);
        Console.WriteLine("The value of May in months " + "enum is " + (int)months.May);
        Console.WriteLine("The value of June in months " + "enum is " + (int)months.June);
Console.WriteLine("The value of June in months " + "enum is " + (int)months.July);
    }
}
}

Output

The value of January in months enum is 0

The value of February in months enum is 1

The value of March in months enum is 2

The value of April in months enum is 3

The value of May in months enum is 4

The value of June in months enum is 5

The value of July in months enum is 6

Let's look at enum rules to understand enums properly.

Enum Rules

  • We can assign one integer value to two enum items in an enum.
  • The compiler gives integer values to the enum items if we do not assign them. The first item is set to 0, and the compiler increments by one for other items in the enum list.
  • We can only assign integer values to the enum items. We can not assign strings as values.

Now, let's move to the FAQs section to clear our doubts regarding enums.

FAQs

  1. Why are enums used in C#?
    In C#, an enum (or enumeration type) assigns constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays. Monday is more readable than number 0 when referring to the day in a week.
  2. Where should enums be defined C#?
    Enums are defined inside a namespace, class, or structure. It is declared using the enum keyword followed by the name of our enum.
  3. Can enum have methods C#?
    C# Does not allow the use of methods in enumerators as it is not a class-based principle but rather a two-dimensional array with a string and value.
  4. Is the enum value type in C#?
    C# enum is a value type with a set of related constants often referred to as an enumerator list. The C# enum keyword is used to declare an enumeration. It is a primitive data type that is user-defined.
  5. Can enums be NULL in C#?
    An enum is a "value" type in C# (means the enum is stored as whatever value it is, not as a reference to a place in memory where the value itself is stored). We can not set value types to null (since null is used for reference types only).

 

Recommended Topic -  singleton design pattern in c#

Key Takeaways

In this article, we extensively discussed enums in C#. We have seen the syntax of enums and how to use enums in our C # program. We have also seen different values an enum can take. In the end, we discussed the rule of enums. 

To read more about reflection in C#, Visit Here.

We hope that this blog has helped you enhance your knowledge regarding enums in C# and if you want to learn more, check out articles on Coding Ninjas Studio. Do upvote our blogs to help other ninjas grow. 

Happy Coding!

Previous article
Structures in C#
Next article
Interfaces in C#
Live masterclass