
C# enums are a common data type, which creates lists populated with constants and their associated values.
Ever thought about just creating a list in C# that can have a drop-down menu displaying all the sub-elements from that list? Well, Enums make C# code easier to read and work with through such a process. C# enums can call lists by name and specify elements from that list.

Here’s What You Need to Know About Using C# Enums
What is an Enum?
The C# enums are formally known as strongly typed constants, which means that they are predefined data types in C#.
The keyword “enum” stands for enumerate, and this data type lives up to its name. As the name suggests, the C# enum keyword is used to declare an enumeration that lists related elements together. It is a useful feature while scripting in C# because grouping related elements together in a list can move the pace of calling and returning elements quickly in a program. This makes it easier to read and write C# code with greater precision.
The resulting list that is defined through the enum keyword is often referred to as an enumerator list. The grouping of related elements in an enumerator list makes it easier to call particular elements later in the program’s script.
How to Classify an Enum?
Enum is a value data type in C#, and value data types are generally stored in the stack. It’s used to declare a list of named constants and their integer values in an application.
Every named constant must have an associated numerical value in the enumerator list, even if that value is the positional count of the constant.
What Should an Enumerator List Contain?
Enumerator lists can include constant elements of many numeric data types like byte, sbyte, short, ushort, int, uint, long, and ulong, but these other types have to be cast.
However, whatever data type is chosen, all the constants have to be of the same data type, e.g. byte or short. The enum data type can be applied by using the enum keyword directly inside the namespace, class, or structure.
The enum keyword is used to name the list of constants and their associated integer values, so when the list is called it can return particular constant values. All members of the defined enumerator list are of the enum type. There must be a numeric value for each enum type.
When Should You Use Enums?
If a program uses a set of integral numbers, then consider replacing them with enums. Using enums can improve the readability of the program, which allows it to become easier to maintain.
If a programmer applies this logic of replacing set integral numbers with enums then going back to read, find, and call these enumerator lists (and their elements) should be less troublesome for the programmer. It makes scripting in C# far easier for both experience and inexperienced programmers.
Enums are useful in creating a list of elements to call on later as a group or individually. They help to provide specificity and scope in selecting elements from the list for various use purposes throughout your C# script.
The best case is to define the enum in the namespace so that all classes in the namespace can access it too. However, it is also valid to add an enum within a class or other structure too. Enums can be defined as global values and local values in the C# script.
How Does It all Work?
Start with typing the keyword enum in the namespace to indicate a named list is soon to follow. Define the list with a name, and then within closed curly braces enter in the constants equated to their associated values.
The elements entered in the list should be separated by commas. Don’t forget to close the list in curly braces instead of flat brackets, which is usually used to define list data types in other programming languages besides C#.
It is also important to remember to end with a semicolon after the closing curly brace.
The Basic Way to Structure an Enum:
Enum <name of enumerated list> { <constant names = values> };
Here’s an Example of How to Use an Enum:
enum WeekDays { Monday = 10, Tuesday = 13, Wednesday = 16 }
What Does this Mean?
To call the value of one of the constant elements in the list, start with calling Console then tacking on the WriteLine method with either the name of the enumerated list or any of the particular elements from the list.
Here’s a Sample of What the Code Looks Like When Calling a Specific Weekday from the Example Enumerator List Above:
Console.WriteLine((int)WeekDays.Monday);
What Does This Mean?
Following the computer science conventions, the first element in the enum list starts count from 0. The count of each successive enum is incremented by 1.
A change in the value of the first constant element in the enum list will reassign the values of the rest of the constant elements to increment accordingly. For example, if the first element in the list is reassigned a value of 10, instead of the default count of 0, then the following element will have a value of 11.
Need More Examples of Enums?
Example 1
Enum Breed { Bulldog, Boxer, Chihuahua, Briar };
What Does this Mean?
In the first example the named constants have countable values associated with them starting from 0. So, the enumerated list, Breed, will have its first value, Bulldog, associated with count 0.
Example 2
Enum Grades { F = 0, D = 1, C = 2, B = 3, A = 4 };
What Does this Mean?
In this second example, the enum has user-assigned values associated with each of the named constants. So, the enumerated list, Grades, has its first value, F, associated with a user-assigned value of 0.
Enum Methods in C#
Enum class contains many useful methods for working with enumerations. The beauty of enum is that you can process it as integer value and display it as string.
Some of the most common enum methods that you can invoke on an enum instance includes the GetName method, GetNames method, Format method, GetUnderlyingType method, and GetValues method.
The GetName Method
This C# enum method returns the name of the constant in the specified enumerated list with a specific value as a string. It takes two parameters as arguments, enum type and value. The first argument, type, has to be specified to set the enumeration type. The second argument, value, is the value of the particular enumerated constant in terms of its underlying type.
The GetNames Method
This C# enum method returns a list/array of constants in the specified enumerated list. It takes one argument, type, which is the enumeration’s type.
The Format Method
This C# enum method converts a specified value of a specific enumerated list to another equivalent data type, usually a string. It takes three parameters as arguments, type, object, and format. The first argument, type, is enum type of the value to convert. The second argument, object, is the value to convert. The third argument, format, is the output format to use.
The GetUnderlyingType Method
This C# enum method returns the underlying type of the specified enumerated list constant value. It takes one argument, type, which is the enum whose underlying type will be retrieved.
The GetValues Method
This C# enum method returns an array of constant values in an enumerated list. It takes one argument, type, which is the enumeration type.

What Else Do I Need to Know About C# Enums?
C# Enums are strongly typed constants. They are strongly typed, i.e., an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums. C# Enums are value types and are created on the stack and not on the heap.
The purpose of the C# enum data type is to make code more readable by grouping related elements together under a defined list name. This list can be called later in the script by name and specify particular constants and their values.