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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - qy

Duwamish7资料收集- [] 学习Duwamish7的MSDN说明及相关技术策略 Duwamish7学习笔记(七) Duwamish7学习笔记(六) Duwamish7学习笔记(五) Duwamish7学习笔记(四) Duwamish7学习笔记(三) Duwamish7学习笔记(二) Duwamish7学习笔记(-) jQuery介绍及语法----转载 浅谈.net事件机制 使用EditPlus2编写C#代码 跟踪用户会话 按钮确认(js与C#交互) 3-28学会的几句sql语句 Visual Studio 2003/Visual Studio 2005常用快捷键 ----转 最优化javascript自定义对象 BOM 关于用div实现table的效果
枚举
qy · 2008-09-19 · via 博客园 - qy

每个枚举都继承至System.Enum,System.Enum继承至System.ValueType,System.ValueType又继承至System.Object

枚举是值类型,枚举不能有方法,属性和事件。
枚举其实就是定义一组常量字段的结构体。

编译器对待枚举类似如下代码
struct Color : System.Enum
{
    public const Color Red = (Color) 0;
    public const Color Red = (Color) 1;
    public const Color Red = (Color) 2;
    public const Color Red = (Color) 3;
}


1.GetUnderlyingType
//返回枚举类型实例值的基础类型
static Type GetUnderlyingType(Type enumType);
//枚举类型实例值的基础类型就是这个枚举索引的类型
//枚举类型实例值的基础类型为byte的枚举
enum Color : byte
{
    Red,
    Green,
}

2.Format
//format参数:G字符串,D十进制,X十六进制,默认是字符串
public static string Format(Type enumType,object value,string format);
//相比ToString()方法的优势:Format可以传递枚举的数值形式
//output "Blue"
Console.WriteLine(Enum.Format(typeof(Color),2,"G"));

3.GetValues
//获得枚举类型中所有符号
static Array GetValues(Type enumType);
Color[] colors = (Color[])Enum.GetValues(typeof(Color));
foreach(Color c in colors)
{
    Console.WriteLine("{0,5:D}\t{0:G}",c);
}
//output
//0  Red
//1  Green

4.GetName,GetNames
//返回数字的字符串表达形式
public static string GetName(Type enumType,object value);
//返回该枚举类型的所有数字的字符串表达形式
public static string[] GetName(Type enumType);

5.Parse
//查询字符串符号对应的数值,ignoreCase参数表示是否区分大小写
public static object Pares(Type enumType,string value,bool ignoreCase);
//0
Color c = (Color) Enum.parse(typeof(Color),"Red",true)

6.IsDefined
//表示每个枚举的类型是否合法
public static IsDefined(Type enumType,int value);
public static IsDefined(Type enumType,string value);

标记枚举

规则:只有当枚举中定义的每一个值是 2 的幂或所定义值的组合时,该枚举才可以显示 FlagsAttribute 属性。

定义
[Flags,Serializable]
public enum WindowStyle
{
    MINIMUM_BUTTON = 1,
    MAXIMUM_BUTTON = 2,
    CLOSE_BUTTON = 4,
    //等价:ALL_BUTTON = MINIMUM_BUTTON | MAXIMUM_BUTTON | CLOSE_BUTTON
    ALL_BUTTON = 7,
}

WindowStyle styles = WindowStyle.MINIMUM_BUTTON | WindowStyle.MAXIMUM_BUTTON;
//output"MINIMUM_BUTTON,MAXIMUM_BUTTON"
Console.WriteLine(styles.ToString());