1. enumeration concept: enumeration is a user-defined Integer type. When declaring an enumeration, you must specify a group of acceptable values that can be included in the enumeration instance, you can also specify a name that is easy to remember. If you try to assign an unacceptable value to an enumeration instance somewhere in the code, compilation will fail.
2. Advantages of enumeration:
(1) Enumeration makes the code easy to maintain and helps to specify valid and expected values for the variable.
(2) Enumeration makes the code clearer. The description name can be used to represent an integer, rather than a fuzzy or variable number.
(3) Enumeration makes the code easy to type.
Example:
// Define an enumeration
Public enum TimeOfDay {
Morning = 0,
Afternoon = 1,
Evening = 2
}
// Main function entry
Static void Main (string [] args)
{
Main ();
Console. ReadLine ();
}
// Call the implementation method
Public static int main (){
WriteGreeting (TimeOfDay. Morning );
Return 0;
}
// Implementation Method
Public static void WriteGreeting (TimeOfDay timeofday) {// pass enumeration as a parameter
Switch (timeofday ){
Case TimeOfDay. Morning:
Console. WriteLine ("Good morning! ");
Break;
Case TimeOfDay. Afternoon:
Console. WriteLine ("Good Afternoon! ");
Break;
Case TimeOfDay. Evening:
Console. WriteLine ("Good Evening! ");
Break;
}
}
Console output result: Good morning!