位枚举(Bit Flags)

场景:如字体,一个字体可以同时拥有枚举里面所列举的一种或者多种风格,这时就需要位枚举

定义:   

 [Flags]
  
public   enum  FontStyle
  
{
      Bold        
= 0x0001,
      Italic        
= 0x0002,
      Regular        
= 0x0004,
      Strikethrough    
= 0x0010,
      Underline        
= 0x0020
  }

Example: 可以通过按位或运算来为字体指定多种风格,如下
 Font f  =   new  Font(
        FontFamily.GenericSansSerif,
        
12.0F ,
        FontStyle.Italic 
|  FontStyle.Underline
        );

枚举变量与某一特定的位枚举成员进行按位与运算,若结果不为0则表明枚举变量中包含着该位枚举成员
static   void  Bar(FontStyle fs)
      
{
          
if ((fs & FontStyle.Bold) != 0)
          
{
              
// Do something associated with bold
          }


          
if ((fs & FontStyle.Italic) != 0)
          
{
              
// Do something associated with italic
          }


          
// Other conditional code continues here
      }



转载于:https://www.cnblogs.com/sundavi/archive/2008/06/24/1229128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值