Enums


What is Enum ?

Enum or EnumerationEnumeration is a user-defined data type in C# language. Enumeration is used to assign names to constants which makes it easy to access constants in a program. There is a keyword enum that gets used to declare Enumeration. You can declare or define integer enums, characters, and boolean in Enumeration.

Syntax of Enumeration

enum enum_name{const1, const2, const3, const4…………….., constn};

The enum keyword is also used to declare or define the enum type variables. Two ways are using which you can declare or define this variable.

  • enum days{sun, mon, tue, wed. thu, fri, sat};
  • enum day, months;

Accessing elements from enum

In enumeration elements are also accessed and stored in a manner. Like, enum num{One, Two, Three};

In the above example, the default value of One is 0 and 1 is of Two to access that element using that specific value. If we want to change these values, then we can change it

enum num{ One = 1, Two = 2, Three = 3, };

After this, the values are changed. You can access One using 1 and Two using 2, and this method helps to access elements easily throughout the program.

Enums get used when we want a fixed valued variable in the whole program, and we can set the values in the enum and access them.