C# Enums
Overview
The article explores C# enums, a powerful feature for representing a set of named constants with meaningful names and associated integral values. It starts with an introduction to C# enums, followed by their declaration and usage examples. Enum members' default and custom underlying values are covered, along with explicit conversion to and from integers.
The article demonstrates using C# enums in switch statements to handle different cases efficiently, enhancing code organization and readability. Overall, the article provides a comprehensive understanding of C# enums and their practical applications in writing clean and maintainable code.
What is C# Enums?
In C#, an enum (enumeration) is a user-defined value type that defines a set of named integral constants. It allows developers to create a collection of related symbolic names representing distinct states or options within their code.
For instance, a deck of playing cards can be represented using an enum named "Suit" with enumerators like Club, Diamond, Heart, and Spade. Similarly, naturally enumerated types like planets, days of the week, colors, and directions can also be represented with C# enums. This allows developers to create more readable and maintainable code by using meaningful symbolic names to represent distinct states or options in their applications.
Example
An enum is created in C# using the enum keyword and is typically placed directly within a namespace, class, or structure. Within the curly brackets, developers can declare the constant names, each separated by a comma.
Syntax:
In the provided syntax, Enum_variable refers to the name of the enumerator, and in C# enum string_1, string_2, and so on are the constant names associated with specific values. The default behavior for enums in C# is to assign the value 0 to the first member, and then each subsequent member's value is incremented by 1. However, developers have the flexibility to modify this default behavior by explicitly specifying custom values for enum members.
Enum in C# with example:
Output:
- Here enum with the name Weekdays is created, and its data members are the name of the week, like Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
- The Main method is the entry point of the program, which will be executed when the program starts running.
- Inside the Main method, the code prints the integer values associated with each day of the week using the Console.WriteLine method.
- The (int) syntax is used to explicitly convert the enumerators to their underlying integer values.
Enum inside a Class
We can also define an enum inside a class in C#. Here we have an enum in c# with an example inside a class:
Example:
Output:
In this example, we have defined the Weekdays enum inside the Program class. It has the same structure as the previous example, but now the Weekdays enum is encapsulated within the Program class.
Enum Values
By default, when defining an enum in C#, the first item has an underlying integer value of 0, the second item has a value of 1, and this pattern continues incrementally for subsequent items.
To obtain the integer value associated with an item in the enum, explicit conversion of the item to an int is required.
Example 1:
Output:
You can also explicitly set the underlying values for enumerators in the enum declaration:
Example 2:
Output:
- In this case, the Status enum has four members (Inactive, Active, Suspended, and Deleted), and their underlying values are explicitly set to 0, 2, 20, and 99, respectively.
- Keep in mind that explicitly setting the underlying values should be done with care, as it may lead to potential conflicts or confusion if the values are not unique or if there are gaps in the sequence.
- Also, avoid changing the underlying values once your enum is being used, as this could break existing code that relies on specific enum values.
Access an Enum
In the C# enum, you can access an enum member using its fully qualified name, which includes the name of the enum followed by the member name separated by a dot. Here's how you can do it:
Assuming you have the following enum defined:
To access a specific enum member, you use the enum name followed by the member name:
In this example, we access the Monday enum member from the Weekday's enum and assign it to a variable called day.
Enum in a Switch Statement
In C# enum, you can use an enum in a switch statement to make your code more readable and maintainable when dealing with different cases represented by enum values. Here's how you can use an enum in a switch statement:
Assuming you have the following enum defined:
Output
- In this example, we have a variable selectedDay of type Weekdays (the enum we defined earlier). We then use a switch statement to handle different cases based on the selected day.
- When selectedDay matches one of the cases (e.g., Weekdays.Monday, Weekdays,Tuesday, etc.), the corresponding code block will be executed.
- In case the selectedDay does not match any of the cases defined in the switch statement, the code within the default block will be executed.
- Leveraging enums in switch statements can significantly enhance code organization and readability, particularly when dealing with a predefined set of cases representing distinct values.
Conclusion
- In this article, we have covered C# enums, which are valuable for defining named constants with associated integral values.
- The main points covered in this article are:
- Enum inside a class
- Access an Enum
- Enum in a Switch Statement
- We also learned how to declare and use enums and access their members.
- C# enums enhance code readability, making it easier to work with a fixed set of related values, such as days of the week or status types. By understanding enums, developers can write more organized and maintainable C# code.