In practical problems, the values of some variables are confined to a limited range. For example, in one weeks only seven days, only 12 months a year, gender only male and female and so on. It is obviously inappropriate to describe these quantities as integers, characters, or other types. For this reason, C # provides a type called an enum. All possible values are enumerated in the definition of the "enum" type, and the variable value that is described as the "enum" type cannot exceed the defined range. It should be stated that an enumeration type is a basic data type, not a constructed type, because it can no longer be decomposed into any basic type.
1. Defining enumerations
Defining enumerations is straightforward, using the enum keyword declaration, for example, to define gender enumeration, gender only male and female
public enum Sex
{
female = 0,//' 0 ' is ' female ' corresponding to the internal expression, can also be said to be female value, ' female ' is an external expression, can also be said to be name
Male = 1,
}
2. Using enumerations
Code1 public void Useenum ()
2 {
3//Gets the value of the enumeration
4 int enumvalue = (int) Sex. Male;//enumvalue value is 1
5
6//Gets the name of the enumeration
7 String enumtext = Sex, male. ToString (),//enumtext value is male
8
9//Convert int type value to corresponding enumeration
Ten int intvalue = 1;//int value
One sex sex = (sex) Intvalue;//sex is the enumeration of the corresponding male
12
13//Convert the name to the corresponding enumeration
string strvalue = "male";
sex = (Sex) enum.parse (typeof (Sex), strvalue);//sex is the enumeration of the corresponding male
16
17//Determine if the int value or name is in the enumeration definition item class
if (enum.isdefined (typeof (Sex), intvalue))//The second parameter can also be passed in strvalue
19 {
20//have
21}
22
//switch to Judge sex (tip: Enter switch and add two tab keys, then switch ()
24//Inside the input enumeration, and then enter, the code snippet will automatically enumerate all the items plus case)
Switch (Sex)
26 {
Case Sex. Female:
break;
Case Sex. Male:
break;
Default:
break;
33}
34//......... ........
35}
3. Usually we are in the database, a lot of state, type, gender, and so on the field to save the numbers, but we need to judge these States in development, the direct use of if (userinfo.sex==0) this way to judge, obviously not very good, if the state of a long time, It's hard to tell which number represents what state. And the code is not significant, we should write code as little as possible to write hard code. If you use an enumeration definition, the database stores the enumeration corresponding to the value, and the name of the enumeration is used when writing the code, so that the code knows what state the database is stored in. Very clear and unambiguous.
The 4.UI layer displays the name of the enumeration. If the database stores an enumerated value (as a number), and the UI is certainly not displayed numerically, the corresponding enumeration name should be displayed. For example, to bind the user's gender in a list of user information (enumerated as sex above), how do you show the name of the enumeration? offer a variety of ways
3.1:gridview Control-bound data source for example, you can add a column of template items to get the enumeration name by value
Code
1 <asp:templatefield headertext= "Gender" >
2 <ItemTemplate>
3 <%# (the namespace where the enumeration resides.) Sex) Convert.ToInt32 (Eval ("Sex"))%>
4 </ItemTemplate>
5</asp:templatefield>
3.2: Get name through Enum object
Code
1 <asp:templatefield headertext= "Gender" >
2 <ItemTemplate>
3 <% #Enum. GetName (typeof (the namespace where the enumeration resides. Sex), Convert.ToInt32 (Eval ("Sex")))%>
4 </ItemTemplate>
5 </asp:TemplateField>
There are many ways to deal with this problem and we are free to choose.
5. The ' Advanced usage ' combination of enumerations: For example, an interface: Add, delete, check, change and other operations, but corresponding to different users have different operating rights. For example, a user can only increase, delete, and B users can only check, change and so on. If one of the field types in the permissions table specifies the user's operation permissions, the problem comes up. Let's take a look at 3 ways to solve the problem:
1. Each operation permission a data, disadvantage: Every time you change the permissions, avoid deleting and add, and the amount of data, if a user has 1000 permissions on behalf of 1000 data, then this table of data can not imagine.
2. A field stores all operation permissions, and each operation permission uses a specified symbol as a delimiter, which is called simple and convenient.
3. Just use our enumeration combination to store all operation permissions in one field, but the value is only one number, unlike the way 2 is separated by a delimiter.
Of course, there are many ways to solve this problem. Let's take a look at how to use enumeration combinations to represent multiple operations permissions.
5.1: Define an Action permission enumeration:
[flags]//must make a mark, marking the system to recognize this enumeration can be used in combination
public enum Role
{
Unassigned = 0,
Delete data = 1,
Modify data = 2,
New data = 4,
View data = 8,
}
5.2: If the user has delete, modify the permissions in the enumeration definition only 1 and 2 of the enumeration, then how to combine the two enumeration values into a single enumeration value stored in the database? Quite simply, take a look at the code:
int allrole = ((int) role. Delete data) + ((int) role. Modify data);
At this point the value of Allrole is 3 (two enumeration corresponding value added: 1+2=3), then directly stored allrole value to the data can be
Now we have to determine whether the user has an operation permission, first remove the permission value from the database, the above, Allrole is the value taken out of the database, is 3, and then we use the bitwise operator to determine:
Code
Role myallrole = (role) allrole;//casts an int value to an enumeration
At this point, the name of Myallrole is ' delete data, modify data ', with a value of 3
Determine if delete permission is available
if (myallrole & role. Delete data) = = role. Delete data)
{
Yes
}
Note that the bitwise operator & method is used here to get the judgment, and the use of the bitwise operator is not discussed here. This is probably the case with combinatorial use. It must be noted that the definition of the enumeration value, we can see the role enumeration of the definition of the law of values, 0 to 1, 1 to 2, 2 to 4, 4 to 8, 8 to 16 ... The current value is 2 times the previous value, why is this defined? Because any combination can be a sum of some of the values in the enumeration range, such as a combined value of 15, that 15 equals the defined value in the enumeration definition to add up to 1, 2, 4, 8, 15=1+2+4+8. The values can be combined into any number only if they are defined by law.
Several points to note in combination:
1. When the enumeration is defined, the [Flags] tag must be marked and the system will determine whether the enumeration can be combined using this tag.
2. The value of the definition enumeration must be defined in terms of the above-mentioned rules, for example: 0, 1, 2, 4, 8, 16, 32 ... you can also use 3-way, for example: 0, 3, 6, 12, 24 ...
6. Summary of Usage Benefits
1. Rules: For example, the value of a field in a database is only 1, 2, 3, 4, such as the state, when we input data, we can take the value from the enumeration, so that the field to avoid other values, but also make the code easier to understand, because when the value is taken, we take the enumeration definition of the name, The name is our custom easy-to-understand Chinese or English.
2. Ease of interpretation: that is, the enumeration name is defined in Chinese and English, when used, then take the name of the enumeration, so a look at the code will know. Instead of writing in code 1, 2, 3, 4 such numbers, perhaps over time to write their own forgotten what 1 stands for? What does 2 mean? , not to mention the maintenance of others in the future.
Transferred from: http://www.360doc.com/content/14/0516/16/10966281_378264063.shtml
. NET enumeration (enum) Usage Summary