標籤:
C#的枚舉類型跟C++差不多,一般我們將enum設為單個狀態,比如enum color_t { RED, BLACK, GREEN}, 只能選擇一個
而有的時候枚舉可以作為位元運算來進行與或運算,比如ControlStyles這個枚舉,看下面一段從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
這裡我自己寫了一段代碼來看個別位是否設定了,注意&的優先順序要比>小,需要括弧起來
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace test4 7 { 8 class Program 9 {10 #region11 public enum person12 {13 Id1 = 1,14 Id2 = 215 }16 #endregion17 static void Main(string[] args)18 {19 person n = person.Id1 | person.Id2;20 //person n = person.id1;21 if ((n & person.Id1) > 0) Console.WriteLine("you 1");22 if ((n & person.Id2) > 0) Console.WriteLine("you 2");23 if ((n & person.Id1) > 0 && (n & person.Id2) > 0) Console.WriteLine("you 1 he 2");24 Console.WriteLine(((int)n).ToString());25 }26 }27 }View Code
C#: enum