<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    http://www.tkk7.com/ebecket 返還網(wǎng)
    隨筆-140  評論-11  文章-131  trackbacks-0

    C# Enumeration
    /* from: http://blog.csdn.net/camel0564/archive/2008/07/31/2748420.aspx */

    1、關于enum的定義
    enum Fabric
    {
    Cotton = 1,
    Silk = 2,
    Wool = 4,
    Rayon = 8,
    Other = 128
    }
    2、符號名和常數(shù)值的互相轉(zhuǎn)換
     
                Fabric fab = Fabric.Cotton;
                int fabNum = (int)fab;//轉(zhuǎn)換為常數(shù)值。必須使用強制轉(zhuǎn)換。
                Fabric fabString = (Fabric)1;//常數(shù)值轉(zhuǎn)換成符號名。如果使用ToString(),則是((Fabric)1).ToString(),注意必須有括號。

                string fabType = fab.ToString();//顯示符號名
                string fabVal = fab.ToString ("D");//顯示常數(shù)值
     
    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、將枚舉作為位標志來處理
    根據(jù)下面的兩個例子,粗略地說,一方面,設置標志[Flags]或者[FlagsAttribute],則表明要將符號名列舉出來;另一方面,可以通過強制轉(zhuǎn)換,將數(shù)字轉(zhuǎn)換為符號名。說不準確。看下面的例子體會吧。注意:
              例一:
              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、枚舉作為函數(shù)參數(shù)。經(jīng)常和switch結(jié)合起來使用。下面舉例
            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
            }

            //將枚舉作為參數(shù)傳遞
            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兩種方法經(jīng)常一起使用,來確定一個值或符號是否是一個枚舉的成員,然后創(chuàng)建一個實例。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);

    8. Enumeration變量的文字描述
    如果想要Enumeration返回一點有意義的string,從而用戶能知道分別代表什么, 則按如下定義:
    using System.ComponentModel;
    enum Direction
    {
        [Description("this means facing to UP (Negtive Y)")]
        UP = 1,
        [Description("this means facing to RIGHT (Positive X)")]
        RIGHT = 2,
        [Description("this means facing to DOWN (Positive Y)")]
        DOWN = 3,
        [Description("this means facing to LEFT (Negtive X)")]
        LEFT = 4
    };

    使用如下方法來獲得文字描述:
    using System.Reflection;
    using System.ComponentModel;
    public static String GetEnumDesc(Enum e)
    {
        FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
        DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[]) EnumInfo.
            GetCustomAttributes (typeof(DescriptionAttribute), false);
        if (EnumAttributes.Length > 0)
        {
            return EnumAttributes[0].Description;
        }
        return e.ToString();
    }

    或者可以自己定義Discription Attributes:(來自:James Geurts' Blog)
    enum Direction
    {
        [EnumDescription("Rover is facing to UP (Negtive Y)")]
        UP = 1,
        [EnumDescription("Rover is facing to DOWN (Positive Y)")]
        DOWN = 2,
        [EnumDescription("Rover is facing to RIGHT (Positive X)")]
        RIGHT = 3,
        [EnumDescription("Rover is facing to LEFT (Negtive X)")]
        LEFT = 4
    }; AttributeUsage(AttributeTargets.Field)]
    public class EnumDescriptionAttribute : Attribute
    {
        private string _text = "";
        public string Text
        {
            get { return this._text; }
        }
        public EnumDescriptionAttribute(string text)
        {
            _text = text;
        }
    }


    本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/kofkyo/archive/2009/02/04/3861359.aspx

    posted on 2009-11-20 17:48 becket_zheng 閱讀(141) 評論(0)  編輯  收藏 所屬分類: C#
    主站蜘蛛池模板: 国产vA免费精品高清在线观看| 亚洲欧洲精品无码AV| 成人免费午夜在线观看| 免费毛片a在线观看67194| 午夜福利不卡片在线播放免费| 99久热只有精品视频免费看| 叮咚影视在线观看免费完整版| 中国毛片免费观看| 免费人成激情视频在线观看冫| 国产真人无码作爱免费视频| 国产性生大片免费观看性| 国产色无码精品视频免费| 免费精品久久天干天干| 一个人免费日韩不卡视频| 日韩视频免费在线观看| 2021精品国产品免费观看| 国产成人福利免费视频| 欧亚精品一区三区免费| 麻豆成人精品国产免费| 免费v片视频在线观看视频| 亚洲成a人片在线观看日本麻豆| 亚洲国产精品日韩专区AV| 亚洲日韩中文无码久久| 亚洲天堂男人天堂| 亚洲乱码一二三四五六区| 亚洲国产精品美女久久久久| 老司机午夜在线视频免费观| 久久高潮一级毛片免费| 人妻无码久久一区二区三区免费| 亚洲黄色免费观看| 在线播放免费播放av片| 亚洲VA综合VA国产产VA中| 亚洲日韩小电影在线观看| 亚洲国产精品网站久久| 亚洲人成网站色7799| 羞羞视频在线观看免费| 久久免费观看国产精品88av| 青青青国产在线观看免费| 国产免费观看青青草原网站| 久久精品国产精品亚洲艾草网美妙| 亚洲AV无码乱码国产麻豆穿越|