C#多枚举值的写法与读法

首先,定义枚举的时候必须是2,4,8,16这种2的次方的值。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public enum TestEnum { None = 1, Value1 = 2, Value2 = 4, Value3 = 8, Value4 = 16, Value5 = 32};


    class Program
    {
        static void Main(string[] args)
        {
            TestEnum test;
            //Detection
            test = TestEnum.Value3 | TestEnum.Value4;
            Console.WriteLine((test & TestEnum.Value2) == TestEnum.Value2);

            //Remove
            test = TestEnum.Value3 | TestEnum.Value4;
            test &= ~TestEnum.Value3;
            Console.WriteLine((test & TestEnum.Value3) == TestEnum.Value3);
            Console.ReadLine();

            //Print:
            //false
            //false
        }
    }
}

 

C#多枚举值的写法与读法

上一篇:学习WPF1.2——WPF布局——了解布局容器


下一篇:C#预处理器指令