欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

位枚举(Bit Flags)

发布时间:2024/7/19 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 位枚举(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

总结

以上是生活随笔为你收集整理的位枚举(Bit Flags)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。