C#: enum

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.