有关C#枚举的问答集锦:使用情景

本文介绍了C#中位枚举的使用,如何通过位运算来指定字体风格,并讨论了如何处理位枚举的检查与赋值问题。此外,还涉及枚举与字符串之间的转换方法,探讨了何时不适合使用枚举的场景。
摘要由CSDN通过智能技术生成
  • 本文汇总了一些有关C#枚举的问答。本文是第三部分,汇总了一些有关枚举的使用情景以及一些限制。
  •  

    之前介绍了C#枚举的基础赋值相关的知识,本文继续介绍有关C#枚举的一些问答。

    Q:我定义了一个这样的枚举:

      
      
       
    1. // Code #20  
    2. public enum FontStyle  
    3. {  
    4.     Bold,  
    5.     Italic,  
    6.     Regular,  
    7.     Strikethrough,  
    8.     Underline  
    9. }  

    我用它来指定字体的风格,但我遇到了麻烦。你知道,字体可以同时拥有枚举里面所列举的一种或者多种风格,那么,我如何为字体同时指定多种风格呢?

    A:这个时候你就需要位枚举(Bit Flags),把Code #20修改一下:

      
      
       
    1. // Code #21  
    2. // I am using the FlagsAttribute to identify a bit flags.  
    3. [Flags]  
    4. public enum FontStyle  
    5. {  
    6.     Bold        = 0x0001,  
    7.     Italic        = 0x0002,  
    8.     Regular        = 0x0004,  
    9.     Strikethrough    = 0x0010,  
    10.     Underline        = 0x0020  
    11. }  

    现在,你可以通过按位或运算来为字体指定多种风格了:

      
      
       
    1. // Code #22  
    2. // See Code #21 for FontStyle.  
    3. Font f = new Font(  
    4.       FontFamily.GenericSansSerif,  
    5.       12.0F,  
    6.       FontStyle.Italic | FontStyle.Underline  
    7.       );  

    --------------------------------------------------------------------------------

    Q:位枚举同样存在类似于Code #15的恶作剧吧?

    A:是的,例如:

      
      
       
    1. // Code #23  
    2. // See Code #21 for FontStyle.  
    3. class Program  
    4. {  
    5.     static void Main()  
    6.     {  
    7.         Bar(FontStyle.Regular | (FontStyle)0x0400);  
    8.     }  
    9.  
    10.     static void Bar(FontStyle fs)  
    11.     {  
    12.         // Code here  
    13.     }  
    14. }  

    --------------------------------------------------------------------------------

    Q:那么,System.Enum.IsDefine方法是否还能应对呢?

    A:不能。位枚举成员并不具备排他性,多个成员可以通过按位或运算组合起来。而System.Enum.IsDefine方法只能判断枚举变量的值是否为某一已定义的枚举成员。请看如下代码:

      
      
       
    1. // Code #24  
    2. // See Code #21 for FontStyle.  
    3. FontStyle fs1 = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;  
    4. Console.WriteLine(Enum.IsDefine(typeof(FontStyle), fs1));  
    5.  
    6. FontStyle fs2 = FontStyle.Regular | (FontStyle)0x0400;  
    7. Console.WriteLine(Enum.IsDefine(typeof(FontStyle), fs2));  
    8.  
    9. // Output:  
    10. // false  
    11. // false  

    我们对代码的输出毫无疑问,因为fs1和fs2都不是一个单独的枚举成员。但这不是我们所追求的答案,我们希望区别对待fs1和fs2,至少我们不希望fs2中的捣蛋家伙——(FontStyle)0x0400——在我们的程序中搞破坏!

    --------------------------------------------------------------------------------

    Q:那么,我们是否有办法隔离这些捣蛋鬼呢?

    A:Of course!我们同样可以使用条件判断语句来处理,但做法将与Code #17和Code #18有所不同。现在我们把Code #23改进如下:

      
      
       
    1. // Code #25  
    2. // See Code #21 for FontStyle.  
    3. class Program  
    4. {  
    5.     static void Main()  
    6.     {  
    7.         Bar(FontStyle.Regular | (FontStyle)0x0400);  
    8.     }  
    9.  
    10.    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值