Unique strength of enumeration, unique strength of enumeration
Enumeration
The enumerated type Declaration defines a type name for a group of related symbol constants. Enumeration is used for "Multiple Choices", that is, when the program is running, it makes a decision from the fixed number of "Choices" set at the time of compilation.
Enumeration type (also called enumeration) provides an effective method to define a group of named Integer constants that can be assigned to variables. For example, if you must define a variable, the value of this variable indicates one day in a week. This variable can only store seven meaningful values. To define these values, you can use the enumeration type. The enumeration type is declared using the enum keyword.
Enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
By default, the basic type of each element in the enumeration is int. You can use a colon to specify another Integer type.
If you do not specify a value for the element in the list of enumerative numbers, their values will automatically increase in increments of 1. In the previous example, the value of Days. Sunday is 0, the value of Days. Monday is 1, and so on. When creating a new Days object, if you do not explicitly assign a value to it, it will have the default value Days. Sunday (0 ). When creating an enumeration, select the most reasonable default value and assign it a zero value. This means that as long as the enumeration is not explicitly assigned a value when it is created, all the enumerated values will have the default value. It must be in lower case or lower case in enumeration, but this is not recommended.
Note: System. the Enum type is the abstract base class of all enumeration types (it is a unique type different from the basic type of enumeration), and. the members inherited by Enum are available in any enumeration type. There is a boxing conversion from any enumeration type to System. Enum, And the unboxing conversion from System. Enum to any enumeration type exists. System. Enum is not an enumeration type. Instead, it is a class type and all enumeration types are derived from it. The type System. Enum is derived from the type System. ValueType, and the latter is derived from the type object. At runtime, the value of the System. Enum type can be null or reference to any enumerated type containing the box value.
Advantages of enumeration:
1. Enumeration makes the code easier to maintain and helps to specify valid and expected values for the variable.
2. Enumeration makes the code clearer. descriptive names can be used to represent integer values rather than fuzzy numbers.
3. Enumeration makes the code easier to type. When assigning values to enumeration instances, VS. net ide uses intelliisense to pop up a list box containing acceptable values, which reduces the number of buttons and allows us to recall possible values.
Enumeration instances
Statement:
public enum TimeOfDay{ Moning = 0, Afternoon = 1, Evening = 2,};
Usage:
Public string getTimeOfDay (TimeOfDay time) {string result = string. empty; switch (time) {case TimeOfDay. moning: result = "Morning"; break; case TimeOfDay. afternoon: result = "Afternoon"; break; case TimeOfDay. evening: result = "Evening"; break; default: result = "unknown"; break;} return result ;}
Enumeration Method
1. Obtain the enumerated string
TimeOfDay time = TimeOfDay. Afternoon; Console. WriteLine (time. ToString (); // output: Afternoon
2. Enum. Parse () method. This method has three parameters. The first parameter is the enumeration type to be used. The syntax is the keyword typeof followed by the enumeration class name placed in parentheses. The typeof operator is described in Chapter 5th. The second parameter is the string to be converted, and the third parameter is a bool, specifying whether to ignore case sensitivity during conversion. Finally, note that the Enum. Parse () method actually returns an object reference -- we need to explicitly convert this string to the desired enumerated type (this is an example of unboxing ). For the above Code, 1 is returned as an object, which corresponds to the enumerated value of TimeOfDay. Afternoon. 1 is generated again when it is explicitly converted to int.
TimeOfDay time2 = (TimeOfDay) Enum. Parse (typeof (TimeOfDay), "afternoon", true); Console. WriteLine (int) time2); // output 1
3. Obtain the name of an enumerated value.
LbOne. Text = Enum. GetName (typeof (TimeOfDay), 0); lbOne. Text = (TimeOfDay) 0). ToString (); // return: Morning
Both methods can be implemented, but when the value is out of bounds (not the value listed in enumeration), there is a certain difference. You can select an appropriate method based on your needs.
LbCon. text = (TimeOfDay) 5 ). toString (); // return value: 5. If this parameter is exceeded, the original value is returned. lbGetName. text = Enum. getName (typeof (TimeOfDay), 5); // return: Null String. If this parameter is exceeded, an empty string is returned.
4. Obtain all enumerated values.
foreach (int i in Enum.GetValues(typeof(TimeOfDay))) lbValues.Text += i.ToString();
5. enumerate all names
foreach(string temp in Enum.GetNames(typeof(TimeOfDay))) lbNames.Text+=temp;
Enumeration and constants
Enumeration is preferred.
In C #, the true strength of enumeration is that they will be instantiated in the background as a structure derived from the base class System. Enum. This indicates that you can call methods to execute useful tasks. Note that because of the execution method of the. NET Framework, there is no performance loss if you use enumeration as a structure in syntax. In fact, once the code is compiled, enumeration becomes the basic type, similar to int and float.
However, in practical applications, you may find that we often define enumeration types in English, because development tools are originally developed in English and used by Americans, you can directly understand the meaning of the enumeration type. In fact, we did one more step during development and needed to translate enumeration types. No way. Anyone who writes the programming language in English. If it is written in Chinese, we don't need to translate it. It is very convenient to use enumeration. For example, TimeOfDay. when Morning sees Morning, the Americans know it is Morning, but for Chinese users, there may be a lot of people who don't understand it. This requires us to translate and explain it and go up to getTimeOfDay () the translation is actually done. Therefore, it is not very convenient to use enumeration. Sometimes we prefer to create constants and declare a set in the class to hold constants and their meanings.
Using constant definition: This method is feasible, but it cannot be guaranteed that the input parameter day is actually limited.
Using System; using System. collections. generic; public class TimesOfDay {public const int Morning = 0; public const int Afternoon = 1; public const int Evening = 2; public static Dictionary <int, string> list; /// <summary> /// obtain the day of the week /// </summary> /// <param name = "day"> </param> /// <returns> </returns> public static string getTimeNameOfDay (int time) {if (list = null | list. count <= 0) {list = new Dictionary <int, string> (); list. add (Morning, "Morning"); list. add (Afternoon, "Afternoon"); list. add (Evening, "");} return list [time] ;}}