How do you declare an enumeration in C#?
In C#, the method of declaring an enumeration is by using the enum keyword. Here is an example of declaring an enumeration.
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
In the example above, we declared an enum type named DaysOfWeek, which contains the days of the week. Each member in the enum has a corresponding integer value, starting from 0 and incrementing automatically. In this example, Monday corresponds to the value 0, Tuesday corresponds to 1, and so on. By default, enum member values start from 0 and increment automatically, but you can also explicitly specify the value for each member.
You can use an enum type to declare variables, parameters, properties, etc., in order to restrict their value range. For example, you can declare a variable and set its value to one of the members in the DaysOfWeek enum.
DaysOfWeek today = DaysOfWeek.Tuesday;
In this way, you can easily use enums to represent a set of related constants and utilize them in your code.