C#中的标志枚举

标志枚举在声明枚举前加[flag] 
枚举值一般用2的N次方(1 2 4 8......) 不能(1 2 3)因为标志每组可以自由组合 1+2=3 就冲突了
下面将其十进制转化成为二进制说明就可以明白点
            00001→1
            00010→2
            00100→4
            01000→8
            10000→16
            --------------------
            高富帅白
            00001
            00010
            00100
            01000
            -------
            01111→15   高富帅白

            --------------------
            01111  高富帅白   用对象集合与上白若结果为白表示真,则要判断的项在集合中
            01000  白
            -------
            01000→8 白

 

namespace _02标志枚举
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo info = new FileInfo(@"D:\a.txt");
            Console.WriteLine(info.Attributes);
            //验证一个文件是否隐藏
            if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
            {
                Console.WriteLine("文件隐藏!!");
            }
            else
            {
                Console.WriteLine("文件没有隐藏张Z!!!");
            }
            //设置文件的特性。只读,隐藏了
            info.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.Archive;
            Console.WriteLine(info.Attributes);
            //验证一个文件是否隐藏
            if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
            {
                Console.WriteLine("文件隐藏!!");
            }
            else
            {
                Console.WriteLine("文件没有隐藏张Z!!!");
            }

           
            GoodPeople people = GoodPeople.帅 | GoodPeople.富 | GoodPeople.高 | GoodPeople.白;
            Console.WriteLine(people);
            //为标志枚举赋值
            //验证枚举people中是否有白这一项
            if ((people & GoodPeople.白) == GoodPeople.白)
            {
                Console.WriteLine("很白!!");
            }
            else
            {
                Console.WriteLine("不白!!!!");
            }
            Console.ReadKey();
        }
    }
    [Flags]//表示标志枚举 ,标志枚举的特性,加上这特性 枚举类型.tostring(),返回的就是文字了,不是数字
    public enum GoodPeople
    {
        //标志枚举是需要设置固定的值的,要是不设置值得话,两个项的或运算就会混乱
        //值必须是2的次方
        //普通枚举是互斥的,但对于标志枚举是可以组合的,
        高 = 1,
        富 = 2,
        帅 = 4,
        白 = 8,
        美 = 16
    }
}

 

posted @ 2013-12-21 14:27  thatday  阅读(834)  评论(0编辑  收藏  举报