zoukankan      html  css  js  c++  java
  • 枚举类型或运算

    1.使用枚举类型进行按位或运算,应该用2的幂(1、2、4、8等) 来定义枚举常量,以确保组按位运算结果与枚举中的各个标志都不重叠;

    2.当可能需要对枚举类型进行按位运算时,应该对枚举使用FlagsAttribute /Flags属性,这样当对枚举使用按位运算时才可以解析出各个具体的枚举常量名,而不仅仅是组合值;

    3. None 用作值为零的标志枚举常量的名称;

    4.如果明显存在应用程序需要表示的默认情况,考虑使用值为零的枚举常量表示默认值。

    示例代码1,不加FlagsAttribute:

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             OprationType opration = OprationType.Read | OprationType.Write;
     6 
     7             Console.WriteLine(opration.ToString());
     8             Console.Read();
     9         }
    10     }
    11 
    12     //[FlagsAttribute]
    13     public enum OprationType
    14     {
    15         None = 0,
    16         Read=1,
    17         Write=2,
    18     }
    View Code

    运行结果:

    示例代码2,加入FlagsAttribute:

     1  class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             OprationType opration = OprationType.Read | OprationType.Write;
     6 
     7             Console.WriteLine(opration.ToString());
     8             Console.Read();
     9         }
    10     }
    11 
    12     [FlagsAttribute]
    13     public enum OprationType
    14     {
    15         None = 0,
    16         Read=1,
    17         Write=2,
    18     }
    View Code

    运行结果:

    5.枚举中的-=操作

    示例代码:

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             OprationType opration = OprationType.Read | OprationType.Write;
     6             opration -= OprationType.Write;
     7             Console.WriteLine(opration.ToString());
     8             Console.Read();
     9         }
    10     }
    11 
    12     [FlagsAttribute]
    13     public enum OprationType
    14     {
    15         None = 0,
    16         Read=1,
    17         Write=2,
    18     }
    View Code

    运行结果:

    参考:http://msdn.microsoft.com/zh-cn/library/system.enum.aspx

  • 相关阅读:
    使用SO_REVTIMEO套接字选项为recvfrom设置超时
    使用select为描述符设置超时
    套接字超时设置方法
    使用SIGALARM为recvfrom设置超时
    使用SIGALARM为connect设置超时
    20200410 阿里巴巴Java开发手册
    20200409 Vue 视频学习【归档】
    20200319 Spring MVC 官方文档【归档】
    20200319 Spring Web MVC 2-5
    20200319 Spring Web MVC 1
  • 原文地址:https://www.cnblogs.com/RoyYu/p/3904379.html
Copyright © 2011-2022 走看看