惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 残叶

ASP.NET 2.0 HttpHandler实现生成图片验证码(转) Asp.net 2.0生命周期 - 残叶 ASP.NET 2.0 的 App_Offline.htm - 残叶 联合使用抽象类和接口 - 残叶 C#中抽象类和接口的区别 C#静态构造函数 封装K2.net 2003中的K2ROM Asp.net开发小技巧 跨页面提交数据的方法 页面的重定向 - 残叶 (转)Asp.net 中 Get和Post 的用法 (转)关于ASP.net运行流程 O/R Mapping (Object-Relation Mapping,对象关系映射) - 残叶 网络主机名与IP C#发送Email方法总结 @@IDENTITY与SCOPE_IDENTITY() .NET和SQL Server中“空值”辨析 - 残叶 Transactions - 残叶 回家了 - 残叶
Enum视为位域
残叶 · 2007-12-05 · via 博客园 - 残叶

    消失了一段时间,重新归来.Copy一段MSDN的讲解.
    位域通常用于由可组合出现的元素组成的列表,而枚举常数通常用于由互相排斥的元素组成的列表。因此,位域设计为通过按位“或”运算组合来生成未命名的值,而枚举常数则不是。语言在对位域的使用和对枚举常数的使用上不同。

   用于 FlagsAttribute 和 Enum 的准则

  • 只有要对数值执行按位运算(AND、OR、XOR)时才对枚举使用 FlagsAttribute 自定义属性。

  • 用 2 的幂(即 1、2、4、8 等)定义枚举常量。这意味着组合的枚举常量中的各个标志都不重叠。

  • 请考虑为常用标志组合创建一个枚举常量。例如,如果用于文件 I/O 操作的枚举包含枚举常量 Read = 1 和 Write = 2,请考虑创建枚举常量 ReadWrite = Read OR Write,该常量组合了 Read 和 Write 标志。此外,在某些情况下,可能会将用于组合标志的按位 OR 运算视为一种高级概念,在简单任务中不需要执行此操作。

  • 将负数定义为标志枚举常量时应谨慎,因为很多标志位置都可能设置为 1,这可能使您的代码产生混淆并易于发生代码错误。

  • 测试数值中是否已设置标志的一种简便方法为:在数值和标志枚举常量之间执行按位“与”操作,这种方法会将数值中与标志不对应的所有位都设置为零,然后测试该操作的结果是否等于该标志枚举常量。

  • 将 None 用作值为零的标志枚举常量的名称。在按位 AND 运算中,不能使用 None 枚举常量测试标志,因为所得的结果始终为零。但是,您可以在数值与 None 枚举常量之间执行逻辑(不是按位)比较,以确定数值中是否已设置任何位。

    如果创建的是值枚举而不是标志枚举,创建 None 枚举常量仍十分有用。原因是在默认情况下,公共语言运行库会将用于枚举的内存初始化为零。因此,如果不定义值为零的常量,则枚举在创建时将包含非法值。

    如果明显存在应用程序需要表示的默认情况,请考虑使用值为零的枚举常量表示默认值。如果不存在默认情况,请考虑使用值为零的枚举常量(这意味着该情况不由任何其他枚举常量表示)。

  • 不要仅为了反映枚举自身的状态而定义枚举值。例如,不要定义仅用于标记枚举末尾的枚举常量。如果需要确定枚举的最后一个值,请显式检查该值。此外,如果枚举常量范围中的所有值都有效,还可以对第一个和最后一个枚举常量执行范围检查。

  • 不要指定保留供将来使用的枚举常量。

  • 在定义采用枚举常量作为值的方法或属性时,应考虑对该值进行验证。原因是即使没有在枚举中定义某个数值,也可以将该数值强制转换为枚举类型。

// Example of the FlagsAttribute attribute.
using System;

class FlagsAttributeDemo
{
    
// Define an Enum without FlagsAttribute.
    enum SingleHue : short
    
{
        Black 
= 0,
        Red 
= 1,
        Green 
= 2,
        Blue 
= 4
    }
;

    
// Define an Enum with FlagsAttribute.
    [FlagsAttribute] 
    
enum MultiHue : short
    
{
        Black 
= 0,
        Red 
= 1,
        Green 
= 2,
        Blue 
= 4
    }
;

    
static void Main( )
    
{
        Console.WriteLine( 
            
"This example of the FlagsAttribute attribute \n" +
            
"generates the following output." );
        Console.WriteLine( 
            
"\nAll possible combinations of values of an \n" +
            
"Enum without FlagsAttribute:\n" );
        
        
// Display all possible combinations of values.
        forint val = 0; val <= 8; val++ )
            Console.WriteLine( 
"{0,3} - {1}"
                val, ( (SingleHue)val ).ToString( ) );

        Console.WriteLine( 
            
"\nAll possible combinations of values of an \n" +
            
"Enum with FlagsAttribute:\n" );
        
        
// Display all possible combinations of values.
        
// Also display an invalid value.
        forint val = 0; val <= 8; val++ )
            Console.WriteLine( 
"{0,3} - {1}"
                val, ( (MultiHue)val ).ToString( ) );
    }
 
}
 

/*
This example of the FlagsAttribute attribute
generates the following output.

All possible combinations of values of an
Enum without FlagsAttribute:

  0 - Black
  1 - Red
  2 - Green
  3 - 3
  4 - Blue
  5 - 5
  6 - 6
  7 - 7
  8 - 8

All possible combinations of values of an
Enum with FlagsAttribute:

  0 - Black
  1 - Red
  2 - Green
  3 - Red, Green
  4 - Blue
  5 - Red, Blue
  6 - Green, Blue
  7 - Red, Green, Blue
  8 - 8
*/