Using a Switch statement with Enum

Enumeration and Switch Case

enum c# vb.net

Numerical constants, commonly known as "nums," exemplify the tenet of strong typing in the field of programming. Their resolute adherence to specific data types furnishes utmost precision and reliability to programming endeavors. When confronting scenarios that demand steadfast and unvarying values, these constants prove exceedingly valuable. Their unwavering nature ensures that once assigned, their values remain impervious to alteration throughout program execution, bestowing stability upon the codebase.

In situations necessitating the use of numerous constant integral values, nums emerge as an efficient and elegant solution, offering the facility to consolidate these values into a singular variable with unparalleled ease and clarity. This fusion not only streamlines the code but also enhances its readability, making it more comprehensible to developers and collaborators alike. The judicious implementation of nums exhibits the virtuosity of skilled programmers, as it underscores their commitment to producing efficient, maintainable, and dependable software solutions.
Syntax
enum <name> { enum list };

Switch Case

C# switch case vb.net

In C#, you can use switch...case statements with enum values to create more concise and readable code when handling different cases based on enum values. To demonstrate this, let's declare an enum called BackColors in a C# program:

using System; public enum BackColors { Red, Green, Blue, Yellow, White } class Program { static void Main() { BackColors selectedColor = BackColors.Green; switch (selectedColor) { case BackColors.Red: Console.WriteLine("Selected color is Red."); break; case BackColors.Green: Console.WriteLine("Selected color is Green."); break; case BackColors.Blue: Console.WriteLine("Selected color is Blue."); break; case BackColors.Yellow: Console.WriteLine("Selected color is Yellow."); break; case BackColors.White: Console.WriteLine("Selected color is White."); break; default: Console.WriteLine("Selected color is not recognized."); break; } } }
In this program, we define an enum BackColors with five color values: Red, Green, Blue, Yellow, and White. Then, we use a switch...case statement to handle different cases based on the value of the selectedColor variable, which is of type BackColors. When you run the program, it will output:
Selected color is Green.
This is because we have assigned BackColors.Green to the selectedColor variable, and the corresponding case BackColors.Green is matched in the switch...case statement, resulting in the message "Selected color is Green." You can change the value of selectedColor to any other BackColors value, and the switch...case statement will handle the corresponding case accordingly.

Conclusion

Using switch...case with enums allows you to create more concise and maintainable code when dealing with multiple cases based on enum values, making your code easier to understand and modify in the future.