Enums provide a simple way to represent a fixed set of related values in a C# program. In this chapter, you will learn what an enum is, its syntax, and how to create and use enums in C# with examples.
An enum (enumeration) is a user-defined data type that is used to store a collection of named constants. It helps represent a fixed set of related values, such as days of the week, months, seasons, colors, or status values.
By default, the first enum member has the value 0, and each subsequent member is assigned the next integer value. You can also assign custom numeric values to enum members if needed.
An enum is declared by using the enum keyword followed by the enum name and its members.
The following syntax is used to declare an enum in C#:
In this syntax,
Let us take an example to illustrate the enum in C# programming.
Output:
WINTER = 0 SUMMER = 2
Explanation:
In this example, we declare an enum called Season with four values. In the Main method, it converts two enum members (WINTER and SUMMER) into integers and outputs their respective values. By default, WINTER is 0, SPRING is 1, SUMMER is 2, and FALL is 3, so it prints 0 for WINTER and 2 for SUMMER.
There are several important features of the C# Enum. Some of them are as follows:
In C# programming, an enum can also be defined inside a class. Let us take an example to illustrate the Enum inside the class and changing the index.
Output:
WINTER = 10 SUMMER = 12
Explanation:
In this example, we declare an enum Season with WINTER as 10, and hence the subsequent members are auto-incremented. In the main() function, it converts WINTER and SUMMER to integers and prints them. The output will be WINTER = 10 and SUMMER = 12.
Let us take an example for weekdays that is declared inside a class in C#.
Output:
Sun = 0 Mon = 1 Sat = 6
Explanation:
In this example, we declare an enum Days consisting of days of the week with values from 0 by default. In the main() function, it converts Sun, Mon, and Sat into their integer values and outputs them. The output will be: Sun = 0, Mon = 1, Sat = 6.
There are several methods and properties of Enum in C#. Some of them are as follows:
In C# programming, the Enum.GetValues is utilized to retrieve an array of all values that are defined in an enum type.
Let us take an example to illustrate the C# enum values to access all values using the getValues() function.
Output:
Sun Mon Tue Wed Thu Fri Sat
In C# programming, the Enum.GetNames() method is utilized to retrieve an array of all the names that are defined in an enum type.
Let us take an example to illustrate the C# enum values to traverse all values using the getValues() function.
Output:
Sun Mon Tue Wed Thu Fri Sat
Explanation:
In this example, we have taken an enum Days with values ranging from Sun to Sat. In the main() function, it retrieves all the enum names as strings using Enum.GetNames() and prints each one separately with a foreach loop, displaying all the days of the week.
In C# programming, this method is used to check whether a given value or name exists in a specified enumeration. It returns a Boolean result.
Let us take an example to illustrate the enum.IsDefined() function in C#.
Output:
True False
In C# programming, Enums are typed constants, but they can be cast explicitly to and from their underlying numeric types like int, byte, etc. We can also convert enums to strings and parse strings to enum values. It is useful when we work with UI, files, or databases where we need to display or store enum names or values.
Let us take an example to convert the Enum in C#.
Output:
Integer value: 1 Enum value: Off String value: On Parsed enum: Off
Explanation:
In this example, we define an enum Status with Off=0 and On=1. It converts an enum member to its integer value, casts an integer back to the enum, converts an enum to a string using the ToString() function, and parses a string into the corresponding enum value using the Enum.Parse() function.
In C# programming, Enums work very well with switch statements. Using an enum within a switch statement allows us to perform different operations depending on the enum value. It helps to improve code readability and maintainability with no hardcoded literals.
It has the following syntax:
Let us take an example to illustrate the enum using a switch statement in C#.
Output:
Midweek day
Explanation:
In this example, we declare an enum Day with the days of the week. In the main() function, it initializes today to Wed and uses a switch to test the value. It prints "Midweek day" because the value is Day.Wed.
The underlying type of an enum in C# is int, which represents each member as a 32-bit signed integer. We can also specify some other integral type, such as byte, sbyte, short, ushort, uint, long, or ulong, to manage memory consumption or to accommodate external data formats.
It has the following syntax:
Let us take an example to specify an Enum type in C#.
Output:
Temperature Level: Normal Underlying Value: 0 Casted from number: Extreme
Explanation:
In this example, we define an enum TemperatureLevel with short as the underlying type, assigning specific numeric values to temperature levels. In the main() function, it assigns a level to Normal and prints out both the name of the enum and its value. After that, it casts the number 50 to the enum, which is equivalent to the value Extreme, and prints it out.
In C#, using the [Flags] attribute on an enum allows you to represent a collection of bit fields. It allows combining enum values with bitwise operators, which proves useful for situations where there are many choices, permissions, or statuses that can be active simultaneously.
Let us take an example to illustrate the flag attribute for a bitwise enum in C#.
Output:
Assigned Rights: Read, Write Has Write Permission: True Updated Rights: Read, Write, Execute After Removing Read: Write, Execute
Explanation:
In this example, we declare an enum A with the [Flags] attribute so that multiple values can be "combined" using bitwise operations. In the main() function, permissions are assigned as Read | Write, which outputs as Read, Write. It queries if Write is present using HasFlag. After that, execute is added, and eventually, read is excluded using bitwise AND and NOT. Lastly, it queries if there are no permissions left and prints accordingly.
We request you to subscribe our newsletter for upcoming updates.