Enumeration
1. Concept and role
(1) For storing constants, assigning values only at the time of definition (preventing malicious tampering during programming and preventing different assignments of the same thing-unifying)
(2) A defined enumeration type needs to contain all possible values of that type
(3) Methods, classes, and interiors can be defined, generally and classes are defined at the same level, so that all classes can be enumerated with this
2. Definition:
Enum type name {value 1, value 2, value 3 ... Value N}
(the default index starts at 0 and the value cannot be of type int)
enum Gender// Define an enumeration type, only "male", "female" two value { male,// when defining an enumeration, do not need double quotes Female}
3. How to use:
Switch (Sex) { case Gender male: Console.WriteLine (" male ") ; Break ; Case Gender. Female: Console.WriteLine (" female "); Break ;}
4. Enum types can be cast to type int---index
= Gender. Male; Console.WriteLine ((int) sex); // Output 0--Index, * * if you write "= 2" on the back of the male, it will output 2, the female will output 3
5. Convert a string to an enumeration type: (user input-judged by try Catch)
(Gender) (Enum.parse (typeof (Gender), "string to convert")//gender write its own defined name
13. C # Basic Grooming (enumeration)