C # Enumeration type,
The enumerated type is composed of a group of Integer constants and supports the following basic types: byte, sbyte, short, ushort, int, unit, long, And ulong. Default Enumeration type: int.
1. one of the advantages of enumeration: in terms of understanding, you can use frequently used values as enumeration types to easily predict the meaning of the Code, such as turning the light status On = 1, Off = 0;
Ii. Make the code more standard to prevent accidental calls;
2. The enumerated type is inherited from the Enum class, which is derived from the ValueType class. Therefore, the enumerated type is the value type.
3. Declare the enumeration type:
enum yiqiok{a,b,c}
In the preceding statement, the values of a, B, and c are 0, 1, and 2.
You can also customize the constant values in the enumeration:
enum yiqiok{a=5,b,c}
The values of a, B, and c are 5, 6, and 7 in sequence.
The unspecified values in enumeration always add one to the previous one. The default value starts from 0.
Declare the basic Enumeration type as non-int:
enum yiqiok :long{a,b,c}
4. Obtain the enumerated Value List:
Class Program {enum yiqiok {a = 9, B = 3, c = 8}; public static void Main () {var values = Enum. getValues (typeof (yiqiok); // use var to declare foreach (int v in values) {Console. write (v + "\ t ");}}}
Result:
5. Obtain the names of the enumerated members:
Enum yiqiok {a = 9, B = 3, c = 8}; public static void Main () {Console. writeLine ("enumeration yiqiok constant 8 name: {0}", Enum. getName (typeof (yiqiok), yiqiok. c); // output the string [] names = Enum. getNames (typeof (yiqiok); foreach (string name in names) // output Member names {Console. write (name + "\ t ");}}
Result: