Enumeration is a very interesting structure. In. net, enumeration types have some cool features.
For example, Enum accessright: int
// Access permission for a certain resource {READ = 1,
// Read The hexadecimal value 0x01 write = 2,
// Write the hexadecimal value 0x02 Delete = 4
// Delete the hexadecimal value 0x04} In. net,
We can: accessright right = accessright. read;
// Obtain the right value of the enumerated variable in decimal format: String S1 = right. tostring ("D ");
// Obtain the hexadecimal string S2 = right. tostring ("X") of the enumerated variable right ");
// Obtain the name of the right enumeration variable, which is readstring S3 = right. tostring ("G ");
If you think the above assignment statement is: (accessright right = accessright. Read;) not flexible enough, you can also do this:
// Use an enumeration name to obtain an enumeration accessright right = (accessright) system. enum. parse (typeof (accessright), "read ")
; // You can also use an enumeration value to obtain an enumeration accessright right = (accessright) system. enum. parse (typeof (accessright), "1 ");
In addition, you can traverse an enumeration type foreach (accessright right in (accessright []).
System. enum. getvalues (typeof (accessright ))){}
Finally, the bitwise algorithm accessright right = accessright. Read | accessright. Write; string Ss = right. tostring ("F ");
Then the SS will be "read, write"