C# 中 enum的總結

來源:互聯網
上載者:User

http://apps.hi.baidu.com/share/detail/23749270

1、關於enum的定義enum Fabric

{Cotton = 1,Silk = 2,Wool = 4,Rayon = 8,Other = 128}
2、符號名和常數值的互相轉換             Fabric fab = Fabric.Cotton;             int fabNum = (int)fab;//轉換為常數值。必須使用強制轉換。            Fabric fabString = (Fabric)1;//常數值轉換成符號名。如果使用ToString(),則是((Fabric)1).ToString(),注意必須有括弧。            string fabType = fab.ToString();//顯示符號名             string fabVal = fab.ToString ("D");//顯示常數值  3、獲得所有符號名的方法(具體參見Enum類)         public enum MyFamily         {             YANGZHIPING = 1,           GUANGUIQIN = 2,              YANGHAORAN = 4,             LIWEI = 8,                 GUANGUIZHI = 16,             LISIWEN = 32, 
     LISIHUA = 64,         }             foreach (string s in Enum.GetNames(typeof(MyFamily)))             {                 Console.WriteLine(s);             }  4、將枚舉作為位標誌來處理
根據下面的兩個例子,粗略地說,一方面,設定標誌[Flags]或者[FlagsAttribute],則表明要將符號名列舉出來;另一方面,可以通過強制轉換,將數字轉換為 符號名。說不準確。看下面的例子體會吧。注意:
          例一:          Fabric fab = Fabric.Cotton | Fabric.Rayon | Fabric.Silk;                    Console.WriteLine("MyFabric = {0}", fab);  //輸出:Fabric.Cotton | Fabric.Rayon | Fabric.Silk;
例二:class FlagsAttributeDemo{     // Define an Enum without FlagsAttribute.         enum SingleHue : short     {         Black = 0,         Red = 1,         Green = 2,         Blue = 4     };
    // Define an Enum with FlagsAttribute.     [FlagsAttribute]
    enum MultiHue : short     {         Black = 0,         Red = 1,         Green = 2,         Blue = 4     };
    static void Main( )    {        Console.WriteLine(             "This example of the FlagsAttribute attribute \n" +             "generates the following output." );   Console.WriteLine(             "\nAll possible combinations of values of an \n" +             "Enum without FlagsAttribute:\n" );                 // Display all possible combinations of values.         for( int val = 0; val <= 8; val++ )              Console.WriteLine( "{0,3} - {1}", val, ( (SingleHue)val ).ToString( ) );          Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum with FlagsAttribute:\n" );                 // Display all possible combinations of values.         // Also display an invalid value.         for( int val = 0; val <= 8; val++ )              Console.WriteLine ( "{0,3} - {1}", val, ( (MultiHue)val ).ToString( ) );     } }
/* This example of the FlagsAttribute attribute generates the following output.
All possible combinations of values of an Enum without FlagsAttribute:
0 - Black 1 - Red 2 - Green 3 - 3 4 - Blue 5 - 5 6 - 6 7 - 7 8 - 8
All possible combinations of values of an Enum with FlagsAttribute:
0 - Black 1 - Red 2 - Green 3 - Red, Green 4 - Blue 5 - Red, Blue 6 - Green, Blue 7 - Red, Green, Blue 8 - 8 */
5、枚舉作為函數參數。經常和switch結合起來使用。下面舉例

        public static double GetPrice(Fabric fab)    

     {             switch (fab)       

      {                 case Fabric.Cotton: return (3.55);           

            case Fabric.Silk: return (5.65);                

        case Fabric.Wool: return (4.05);             

            case Fabric.Rayon: return (3.20);           

              case Fabric.Other: return (2.50);          

               default: return (0.0);             }         }

6、上面三點一個完整的例子

        //1、enum的定義         public enum Fabric : short   

      {             Cotton = 1,             Silk = 2,             Wool = 3,             Rayon = 8,             Other = 128         }

        //將枚舉作為參數傳遞         public static double GetPrice(Fabric fab)     

    {             switch (fab)         

    {               

    case Fabric.Cotton: return (3.55);                

   case Fabric.Silk : return (5.65);             

      case Fabric.Wool: return (4.05);        

         case Fabric.Rayon: return (3.20);         

        case Fabric.Other: return (2.50);          

       default: return (0.0);             }         }

        public static void Main()      

   {             Fabric fab = Fabric.Cotton;          

         int fabNum = (int)fab;      

         string fabType = fab.ToString();         

        string fabVal = fab.ToString ("D");        

     double cost = GetPrice(fab);           

  Console.WriteLine("fabNum = {0}\nfabType = {1}\nfabVal = {2}\n", fabNum, fabType, fabVal);        

     Console.WriteLine("cost = {0}", cost);
        }

7、Enum類的使用

Enum.IsDefinde、Enum.Parse兩種方法經常一起使用,來確定一個值或符號是否是一個枚舉的成員,然後建立一個執行個體。Enum.GetName列印出一個成員的值;Enum.GetNames列印出所有成員的值。其中注意typeof的使用。這一點很重要。

        public enum MyFamily     

    {             YANGZHIPING = 1,             GUANGUIQIN = 2,             YANGHAORAN = 4,             LIWEI = 8,             GUANGUIZHI = 16,             LISIWEN = 32,
            LISIHUA = 64,         }

            string s = "YANGHAORAN";       

      if (Enum.IsDefined(typeof(MyFamily), s))             {                 MyFamily f = (MyFamily)Enum.Parse(typeof(MyFamily), s);                 GetMyFamily(f);                 Console.WriteLine("The name is:" + Enum. GetName(typeof(MyFamily), 2));                 string[] sa = Enum.GetNames(typeof(MyFamily));
                foreach (string ss in sa)                 {                     Console.WriteLine(ss);                 }             }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.