Enumerations
Contents
- Question/Answer
- 1. What is an enumeration in C#?
- 2. What is the general form of an “enumeration” type? The enum keyword
- 3. In parts of the program can you declare the type of “enumeration”
- 4. Examples of declaring the enumerations and their use in the program
- 5. How is the enumeration initialized? Example
- 6. Which basic type have constants in the enumeration by default?
- 7. How can I change the basic type of constants in the enumeration?
- 8. What are the advantages of using enumerations in the programs?
- 9. An example of using an enumeration with a switch statement
- Related topics
Search other websites:
1. What is an enumeration in C#?
Enumerations in C# are a set (sequence) of constants that form a data type. The value of each constant is given by a name. In C#, enumerations are represented by a class. All enums are inherited from the System.Enum class.
⇑
2. What is the general form of an “enumeration” type? The enum keyword
The general form of the declaration of type “enumeration” is:
enum name { list_of_constants };
where
- name – this is the name of the enumeration type;
- list_of_constants – a list of identifiers, separated by commas.
⇑
3. In parts of the program can you declare the type of “enumeration”
The “enum” type can be declared within the namespace within the class. In the body of the method, it is not possible to declare the type of an enumeration.
⇑
4. Examples of declaring the enumerations and their use in the program
Example 1. The Month declaration that defines the list of months of the year.
// Declare type "Month" enum Month { January, February, March, April, May, June, July, August, September, October, November, December }
Using the Month type in another code (for example, an event handler)
// use of enumeration in the program code Month M; // Declaring an M variable of type "enumeration" M = Month.January; // M = January int m; m = (int)M; // m = 0 M = Month.August; // M = August m = (int)M; // m = 7
Example 2.
A DayWeek type declaration containing information about the day of the week.
enum DayWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
An example of using composite assignment operators with a variable of type DayWeek:
DayWeek D; D = DayWeek.Sunday; D++; // D = Monday D += 2; // D = Wednesday D += 10; // D = 13 D = DayWeek.Friday; --D; // D = Thursday D = D - 8; // D = -4
⇑
5. How is the enumeration initialized? Example
Example 1. Initializing an enumeration of the Month type, in which each month is initialized with a value equal to the number of days in this month.
Declaring of enumeration:
// Declare the 'Month' type enum Month { January = 31, February = 28, March = 31, April = 30, May, June = 30, July, August = 31, September = 30, October, November = 30, December }
Demonstration of using a variable of type Month
Month M; int days; M = Month.December; // days = (int)M; // days = 31 M = Month.October; days = (int)M; // days = 31 M = Month.February; days = (int)M; // days = 28
Example 2. Declaration of the enumeration, in which the day of the week has its number starting on Monday. A rule is shown: each next constant is assigned a value that is 1 greater than the value of the previous initialized constant.
Declaring a DayWeek type enumeration
enum DayWeek { Sunday = 7, Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday }
Demonstration of use in the program
DayWeek DW; // variable of type DayWeek DW = DayWeek.Saturday; // Determining whether a day off or working day if ((DayWeek.Monday <= DW) && (DW <= DayWeek.Friday)) label1.Text = "Working day"; else label1.Text = "Day off"; DW = DayWeek.Thursday; int num = (int)DW; // num = 4 num = (int)DayWeek.Tuesday; // num = 2
⇑
6. Which basic type have constants in the enumeration by default?
By default, the base type for constants in the enumeration is int.
⇑
7. How can I change the basic type of constants in the enumeration?
To change the base type of constants from int to another, you must specify this type immediately after the name of the enumeration. The general form of such enumeration will look like:
enum name : new_type { list_of_constants };
where
- name – this is the name of the enumeration type;
- new_type – in fact, a new type that is installed in place of the base type;
- list_of_constants – a list of identifiers, separated by commas.
Example. A type declaration is given that defines the grades in the disciplines. In the enumeration declaration, the constants are of type byte.
enum Marks : byte { unsatisfactory = 2, satisfactory, good, excellent }
Using a Marks variable in the program
Marks M; M = Marks.good; byte b; b = (byte)M; // b = 4 b = (byte)Marks.excellent; // b = 5
⇑
8. What are the advantages of using enumerations in the programs?
The use of enumerations is effective if the program often uses constants that have symbolic designations. Using enumerations instead of numeric values has the following advantages:
- increasing understanding and readability of program code, since the reference is not to numbers, but to semantic names;
- improving control over the validity of names and their values. Using enumerations ensures that variables will have values from the expected (valid) set of values. If you try to assign a value not declared in the enumeration, the compiler will throw an error;
- saving programmer time. When using names, IntelliSense displays a convenient list of all valid names in an enumeration. Simply select the desired value from the list;
- expansion of functionality. Since enumerations are inherited from the System.Enum class, several useful methods implemented in this class are added to them;
- reducing the number of possible errors in the program.
Also, the use of enumerations allows group the constants in order to further operate them. This, in turn, streamlines the program code and reduces the risk of making a mistake by the programmer.
⇑
9. An example of using an enumeration with a switch statement
The example uses the Month enumeration to determine the number of days in a month.
Let the ‘Month’ enumeration declarations are given
// declare the Month type enum Month { January, February, March, April, May, June, July, August, September, October, November, December }
Using a variable of type ‘Month’ in the program
// Determining the number of days in a month Month M; int n_days; M = Month.November; switch (M) { case Month.February: n_days = 28; break; case Month.April: case Month.June: case Month.September: case Month.November: n_days = 30; break; default: n_days = 31; break; } // display to the form label1.Text = n_days.ToString();
⇑
Related topics
- Structures. General concepts. Type of structure. Structural variable. Modifiers of access to structured variables. Examples of using the structures
- Interfaces. The use of interfaces in C# programs