Traverse an enumeration type

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.