C #: enum

Source: Internet
Author: User

C # enum type is similar to C + +, generally we set the enum to a single state, such as enum color_t {RED, BLACK, GREEN}, can only select one

And sometimes enumerations can be used as bitwise operations to perform with or operations, such as the ControlStyles enumeration, to see a section of code from Tabcontrolex

1            Base . SetStyle (2                 controlstyles.userpaint |  3                 Controlstyles.optimizeddoublebuffer |  4                 Controlstyles.allpaintinginwmpaint |  5                 Controlstyles.resizeredraw |  6                controlstyles.supportstransparentbackcolor,7                 true); 8             base. UpdateStyles ();       
View Code

Here I wrote a piece of code to see if the individual bit is set, note that the & priority is smaller than >, need parentheses up

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespacetest47 {8     class Program9     {Ten         #region One          Public enum Person A         { -ID1 =1, -ID2 =2 the         } -         #endregion -         Static voidMain (string[] args) -         { +person n = person. Id1 |Person . ID2; -             //Person n = person.id1; +             if(N & Person. ID1) >0) Console.WriteLine ("You 1"); A             if(N & Person. ID2) >0) Console.WriteLine ("You 2"); at             if(N & Person. ID1) >0&& (n & person. ID2) >0) Console.WriteLine ("You 1 He 2"); -Console.WriteLine ((int) n). ToString ()); -         } -     } -}
View Code

C #: enum

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.