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