Home >Backend Development >C#.Net Tutorial >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 };
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.
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.
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:
Given below are the enum methods in C#:
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:
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:
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:
Given below are the rules for the enum in C#:
Given below are the advantages mentioned:
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.
The above is the detailed content of Enum in C#. For more information, please follow other related articles on the PHP Chinese website!