C # learning notes (basic knowledge Review) enumeration,
I. Meanings of enumeration
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 example. You can also specify a name that is easy to remember. Personal Understanding is to assign meaning to a set of integer values.
Ii. Enumeration advantages
2.1: enumeration makes the code easier to maintain and helps to specify valid and expected values for the variable.
2.2: enumeration can make the code clear and use descriptive names to represent integer values, increasing code readability.
Iii. Use of enumeration
/// <Summary> /// declare an enumeration, audit status /// </summary> public enum AduitStatus {draft = 0, pending Review = 1, approved = 2, review returned = 3} public static void AuditResource (AduitStatus aduitStatus) {switch (aduitStatus) {case AduitStatus. approved operations: // such as modifying the status of the record in the database. writeLine ("approved! "); Break; case AduitStatus. Review returned: // review returned operation Console. WriteLine (" returned! "); Break; default: Console. WriteLine (" hello "); break ;}}4. When do I use enumeration in my project?
4.1 people understand that enumeration is used to store a set of values that will not be changed easily, such as gender: Unknown, male, and female. Review status: Draft, approved, and rejected.
4.2 taking the review status as an example, the database generally stores the values of each status. The status name must be displayed when the interface is displayed. For example, 0 indicates the draft, 1 indicates that the review is passed, and 2 indicates that the review is returned. If you do not need enumeration, you can also implement the following:
Public static void AuditResource (int aduitStatus) {switch (aduitStatus) {case 1: // approved operations, such as modifying the status of the record in the database and so on. writeLine ("approved! "); Break; case 2: // audit return operation Console. WriteLine (" returned! "); Break; default: Console. WriteLine (" hello "); break ;}}
When displayed, replace the obtained value with the descriptive name with the code value.
Foreach (Article article in list) {string ArticleStatus = ""; if (article. status = 0) {ArticleStatus = "draft";} else if (article. status = 1) {ArticleStatus = "approved";} else if (article. status = 2) {ArticleStatus = "review returned ";}
//......
}
The above hard encoding has many drawbacks. If others read the above Code separately, they may not understand the meaning of each number, even after a long time, you may not be able to remember the meaning of each value. It is hard to understand when you read the code here, and there is no standard name for display, different people have their own names for describing different states.
As in the case where the Switch is used above, it is often possible to use enumeration. For example, in the following code, the value in the swicth statement is implemented by enumeration. When the status name in a class is displayed, the value is converted to the name in the enumeration. In this way, the code is easier to understand and the display will be unified.
Class Program {// <summary> // declare an enumeration, audit status /// </summary> public enum AduitStatus {draft = 0, approved = 1, review returned = 2} public class Artice {public string Title {get; set;} public int status {get; set ;}} static void Main (string [] args) {Console. writeLine ("is this resource approved? (Y/N) "); string isPass = Convert. toString (Console. readLine (); if (isPass. toUpper () = "Y") {AuditResource (AduitStatus. approved);} else {AuditResource (AduitStatus. review returned);} Artice Ar = new Artice () {Title = "C # Study Notes", status = 0}; Console. writeLine ("title: {0}, status: {1}", Ar. title, (AduitStatus) (Ar. status); // output "title: C # learning notes, status: Draft" Console. readKey ();} public static void AuditResource (AduitStatus aduitStatus) {switch (aduitStatus) {case AduitStatus. approved operations: // approved operations, such as Console for modifying the status of the record in the database. writeLine (AduitStatus. approved); break; case AduitStatus. review and return: // review and return operation Console. writeLine (AduitStatus. review and return); break; default: Console. writeLine ("hello"); break ;}}}
Allowed results
4.3 again, for example, gender, the front-end may be implemented using a single-choice box. Of course, the male and female options can be listed on the page and the values of each item can be written. Its disadvantages are similar to those above, in this case, enumeration can also be used for definition.
5: Other solutions for using a set of meaningful values
For example, the types of key urban projects: infrastructure projects, industrial projects, and manufacturing projects may be added in the second year, such as service projects. Although a group of values like this can also be done using enumeration, you need to adjust the code when adding a new project type in the next year. This is something that may change at the beginning, is a normal increase, should not adjust the program code. Such a group of easy-to-change values are generally stored in a data dictionary table, and these code items can be dynamically configured.
A data dictionary table generally uses a master table and a sub-table. The master table stores all the configuration item names in the system, such as the book category and major project type. The sub-Table stores the values of this class, such as names, values, sorting, and levels. Other tables that are useful for these code values are associated with these two types of tables.In short, it is best to maintain an uncertain set of meaningful values in a configurable manner. A set of meaningful values that are not easily changed can be implemented using enumeration.