Enumeration (enum) C#
Enum stands for enumeration. In C#, an enum
(enumeration) is a user-defined data type that allows you to define a set of named constants. By default, these constants are assigned integer value starting from , with added constants being by 1.
Let’s continue with the example of a laptop to illustrate enumeration in C#:
enum LaptopBrand
{
Acer,
HP,
Dell,
Lenovo,
Apple,
Asus
}
In this example, we define an enum
called LaptopBrand
with five constants representing different laptop brands: Acer, HP, Dell, Lenovo, Apple, and Asus.
You can use this enum
to declare variables that hold one of the defined brand values:
LaptopBrand myLaptopBrand= LaptopBrand.Acer;
Console.WriteLine("My laptop brand is: " + myLaptopBrand);
// Output
// My laptop brand is: Acer
Advantages of Enumeration:
- Code Clarity
- Easier maintenance
- Readability
Thank you! I’m glad to hear that you’re enjoying the learning process. If you have any more questions or need further assistance, feel free to ask. Happy learning to you too!