枚举关于Flags特性的使用

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

namespace _02EnumTest
{
    [Flags]//特性,表示可以将枚举对象视为位标志
    enum CardDeckSetting : uint
    {
        SingleDeck    = 0x01, //位0:0000 0001
        LargePrctures = 0x02, //位1:0000 0010
        FancyNumbers  = 0x04, //位2:0000 0100
        Animation     = 0x08  //位3:0000 1000
    }
    class EnumSource
    {
        bool UseSingleDeck = false,
             UseBigPics = false,
             UseFancyNumbers = false,
             UseAnimation = false,
             UseAnimationAndFancyNumbers = false;

        //判断当前位是否包含在位集合中
        public void SetOptions(CardDeckSetting ops)
        {
            UseSingleDeck = ops.HasFlag(CardDeckSetting.SingleDeck);
            UseBigPics = ops.HasFlag(CardDeckSetting.LargePrctures);
            UseFancyNumbers = ops.HasFlag(CardDeckSetting.FancyNumbers);
            UseAnimationAndFancyNumbers = ops.HasFlag((CardDeckSetting.Animation | CardDeckSetting.FancyNumbers));
        }

        //使用FIags特性,可以把一个枚举标志字全部遍历出来
        public void ShowCDSCollection()
        {
            CardDeckSetting ops = CardDeckSetting.Animation | CardDeckSetting.FancyNumbers | CardDeckSetting.LargePrctures;
            Console.WriteLine(ops.ToString());
        }

        //字符串转为枚举值
        public CardDeckSetting StringToEnum(string stringVal) => (CardDeckSetting)Enum.Parse(typeof(CardDeckSetting), stringVal);

        //索引器
        public bool this[int index]
        {
            set {
                if (index<4)
                {
                    switch (index)
                    {
                        case 0:
                            UseSingleDeck = value;
                            break;
                        case 1:
                            UseBigPics = value;
                            break;
                        case 2:
                            UseFancyNumbers = value;
                            break;
                        case 3:
                            UseAnimation = value;
                            break;      
                    }
                }
                else
                {
                    Console.WriteLine("超出索引范围(0-3)");
                }

            }
            get {
                if (index < 4)
                {
                    switch (index)
                    {
                        case 0:
                          return UseSingleDeck;                          
                        case 1:
                          return UseBigPics;
                        case 2:
                          return UseFancyNumbers;
                        case 3:
                          return UseAnimation;
                        default:
                            return false;
                    }
                }
                else
                {
                    Console.WriteLine("超出索引范围(0-3)");
                    return false;
                }
            }
        }

        //使用Enum的GetNames枚举进行迭代输出
        public void  ShowEnumAll()
        {
            foreach (var item in Enum.GetNames(typeof(CardDeckSetting)))
            {
                Console.WriteLine(item);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02EnumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CardDeckSetting ops = CardDeckSetting.Animation | CardDeckSetting.FancyNumbers | CardDeckSetting.SingleDeck;
            EnumSource es = new EnumSource();

            Console.WriteLine("==========位标志判断===========");
            es.SetOptions(ops);
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(es[i]);//使用索引器
            }
            Console.WriteLine();
            Console.WriteLine("=========字符串转换位枚举============");
            Console.WriteLine(es.StringToEnum("SingleDeck"));
            Console.WriteLine();
            Console.WriteLine("=========使用Enum.GetNames()遍历所有的枚举对象============");
            es.ShowEnumAll();
            Console.ReadKey();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值