Updated May 26, 2023
Introduction to Enum in C#
Enum is a reserved keyword in C#, which represents an enumeration. An Enumeration is a user-defined type in C# that holds a set of named constants. Each constant in the set has a value (by default, integers). The constants can be accessed through both names and values. Thus, the names cannot be duplicated in an enum. The syntax of an enum is straightforward. It starts with the keyword enum, followed by a variable name and a set of named constants wrapped in curly brackets. Of course, it ends with a semi-colon.
Syntax:
enum <enum_name> {<set_of_named_constants>};
Example:
Code:
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
Working of Enum in C#
Enums have an underlying type in C#. This means that every constant in the enum will have a value of the underlying type assigned to it. The default underlying type of enum in C# is an integer.
In the above enum Day, we have seven constants declared. It has a default underlying type of integer. Thus, every constant will have an integer value assigned to it. By default, the value starts at 0. Thus, Sun will have 0 assigned to it; Mon will have 1 assigned to it, Tue will have 2 assigned to it, and so on.
Let us take another example. We would assign the value 1 to Sun. This would force the compiler to start the assignment from 1 instead of 0. Then, we would assign the value 8 to Thu. The compiler would then continue the assignment from 8 onwards.
Example #1
Code:
using System;
public class Program
{
enum Day
{
Sun = 1,
Mon,
Tue,
Wed,
Thu = 8,
Fri,
Sat
};
public static void Main()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine((Day)i);
}
}
}
Output:
A real-world example can be the various stage in a client’s journey. Of course, all the constants can be declared separately and assigned corresponding integer values to it, but using an enum eliminates the hassle of remembering the integer values for each stage and makes the code much easier to understand.
Example #2
Code:
using System;
public class Program
{
enum ClientJourney
{
Introduced = 1,
UnderReview = 2,
Reviewed = 3,
Documentation = 4,
Onboarded = 5,
Declined = 6,
Deboarded = 7
};
public static void Main()
{
Console.WriteLine("The various stages in the journey of a client are:\n");
foreach (string str in Enum.GetNames(typeof(ClientJourney)))
{
Console.WriteLine(str);
}
}
}
Output:
Enum Methods in C#
Given below are the enum methods in C#:
1. GetName(Type, Int32)
The GetName method returns the named constant mapped to the specified value in the enum. If there is no named constant at that value, it returns a blank string.
This method expects two parameters – one is the type i.e. the enum itself, and the other is the specified value.
In the same example of Days above, we would print the named constants through the GetName method.
Code:
using System;
public class Program
{
enum Day
{
Sun = 1,
Mon,
Tue,
Wed,
Thu = 8,
Fri,
Sat
}
;
public static void Main()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Enum.GetName(typeof(Day), i));
}
}
}
Output:
2. GetNames(Type)
In the above example, we had to use the for loop to retrieve all the named constants of the enum. The GetName method is more useful when we need a single or a limited number of named constants.
We have another method GetNames() to get all the named constants. It takes the enum as the input parameter.
In the same example of Days above, we would print the named constants through the GetNames method.
Code:
using System;
public class Program
{
enum Day
{
Sun = 1,
Mon,
Tue,
Wed,
Thu = 8,
Fri,
Sat
}
;
public static void Main()
{
foreach(string namedConstant in Enum.GetNames(typeof(Day))){
Console.WriteLine(namedConstant);
}
}
}
Output:
3. GetValues(Type)
The GetValues() method is used to get the underlying mapped values to each of the enum’s named constants.
In the same example of Days above, we would print the named constants through the GetNames method.
Code:
using System;
public class Program
{
enum Day
{
Sun = 1,
Mon,
Tue,
Wed,
Thu = 8,
Fri,
Sat
}
;
public static void Main()
{
foreach (int i in Enum.GetValues(typeof(Day)))
{
Console.WriteLine(i);
}
}
}
Output:
Rules for Enum in C#
Given below are the rules for the enum in C#:
- Enum is a type, not a variable/constant/method.
- Enum-named constants cannot be duplicated. They must be unique.
- The underlying type of enum cannot be char.
- The default underlying type of enum is an integer.
- By default, the values assigned to enum-named constants start from 0, increasing by 1 for each successively named constant.
Advantages of Enum in C#
Given below are the advantages mentioned:
- Enums are a very good coding practice. They are used to define the set of constants for the application or the program. Thus, the set remains constant and avoids chaos in the program, especially when it is a vast application involving multiple developers.
- There are many scenarios in which enums can be used. Days of the week, months of the year, conversion ratios for temperatures, conversion factors for time zones, or any master table data for the application. This makes the code more readable and easier to understand.
Conclusion
Thus, we have seen the use of enum in C#. We have seen how it works and what can be its practical use. We understood the rules to comply with when using enums. Some methods help us access and format the enum. It is recommended to use enums in your code as much as possible. This will help you observe coding good practices and learn enums as well.
Recommended Articles
This is a guide to Enum in C#. Here we discuss the introduction to Enum in C#, working, methods, rules, and advantages. You may also look at the following articles to learn more –