首页 > 代码库 > C#多枚举值的写法与读法

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

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03


首先,定义枚举的时候必须是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
        }
    }
}

 

参考页面:http://qingqingquege.cnblogs.com/p/5933752.html

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