CLR via C#(18)——Enum
1. Enum定义
枚举类型是经常用的一种“名称/值”的形式,例如:
public enum FeedbackStatus
{
New,
Processing,
Verify,
Closed
}
定义枚举类型之后我们在使用时方便了许多,不用再记着0代表什么状态,1代表什么状态。而且枚举类型时强类型的,在编译时就可以进行类型安全检查。枚举类型是值类型的,它是直接从System.Enum继承的,System.Enum又是继承自System.ValueType。但是枚举类型不可以定义方法、属性或者事件。
2. 常用方法
①Enum.GetUnderlyingType:获取枚举类型实例值的基类。
Console.WriteLine(Enum.GetUnderlyingType(typeof(FeedbackStatus)));//结果System.Int32
②ToString() :转换为字符串形式
FeedbackStatus status=FeedbackStatus .New ;
Console.WriteLine(status.ToString()); //结果New
Console.WriteLine(status.ToString("G")); //结果New
Console.WriteLine(status.ToString("D")); //结果0
③GetValues:获取枚举类型中定义的所有符号以及对应的值。
FeedbackStatus[] status = (FeedbackStatus[])Enum.GetValues(typeof(FeedbackStatus));
foreach(FeedbackStatus s in status )
{
Console.WriteLine("{0:D}--{0:G}", s);
}
④GetNames:获取枚举类型中定义的所有符号。
string[] arr= Enum.GetNames(typeof(FeedbackStatus));
foreach (string name in arr)
{
Console.WriteLine(name);
}
⑤Parse, TryParse:将文本类型转换为对应的枚举类型。
FeedbackStatus status = (FeedbackStatus)Enum.Parse(typeof(FeedbackStatus), "New", false);
Enum.TryParse("aaa", false, out status);
⑥IsDefine:判断一个值对于一个枚举类型是否合法。
Console .WriteLine(Enum.IsDefined(typeof(FeedbackStatus),1)); //true
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "New"));//true
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "new"));//false,区分大小写
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "aaa"));//false
Console .WriteLine(Enum.IsDefined(typeof(FeedbackStatus ),5)); //false
3. 扩展方法与枚举
上面提到过枚举中是不允许定义方法和事件的。但是我们可以通过扩展方法变相的为枚举添加方法。
public static class EnumMethod
{
public static void Show(this FeedbackStatus status)
{
string[] arr = Enum.GetNames(typeof(FeedbackStatus));
Console.WriteLine("枚举类型列表:");
foreach (string name in arr)
{
Console.WriteLine(name);
}
}
}static void Main(string[] args)
{
FeedbackStatus status = FeedbackStatus.Processing;
status.Show();}
CLR via C#(18)——Enum的更多相关文章
- [CLR via C#]18. Attribute
attribute可以说是Microsoft .NET Framework提出的最具创意的技术之一了.利用attribute,可以声明性的为自己的代码构造添加注解,从而实现一些特殊的功能.attrib ...
- Rhythmk 一步一步学 JAVA (18): Enum枚举学习
枚举定义: public enum SizeEnum { SMALL, BIG, BIGEST }; public enum SizeStringEnum { SMALL("小") ...
- 一百多道.NET面试题!
1.a=10,b=15,在不用第三方变量的前提下,把a,b的值互换 方法一: a=a+b; b=a-b; a=a-b; 方法二: a^=b^(b^=a^b); 2.已知数组int[] max= ...
- .Net Core 之 图形验证码 本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- .Net Core 之 图形验证码
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- [转载] TLS协议分析 与 现代加密通信协议设计
https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...
- MVC中验证码的生成
在项目中验证码的生成通常是需要页面无刷新的,所以验证码图片实际是跟在某个input后面的img,通过控制该img来控制验证码显示的位置,例如: <div> <input id=&qu ...
- C++转义字符 & keyword
转义字符: 换行符 \n 水平制表符\t 纵向制表符 \v 退格符 \b 回车符 \r 进纸符 \f 报警(响铃)符 \a 反斜线 \\ 疑问号 \? 单引號 \' 双引號 \" ...
- Visual C++2010开发权威指南 中文高清PDF - VC.NET
第一部分 Visual C++ 2010开发与新特性第1章 Visual C++ 2010开发环境简介 11.1 Visual C++ 2010简介 11.2 Visual C++ 2010下 ...
随机推荐
- BZOJ 3736: [Pa2013]Karty
Description 一个0/1矩阵,求能覆盖所有 \(1\) ,同时不覆盖所有 \(0\) 的矩阵,使这个面积最大. Sol DP/悬线法. 首先,所求的矩阵一定可以覆盖所有贴边的悬线. 用悬线法 ...
- zabbix之MySQL数据库的安装
转载自:http://www.ttlsa.com/mysql/install-mysql5_6/ 启动MySQL并制定套接字 mysql -u user -ppassword -S 指定socket路 ...
- Android studio教程
Android studio教程: http://jingyan.baidu.com/season/44062
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- Python之调用WebService:suds
suds官方: https://fedorahosted.org/suds/wiki/Documentation 互联网公开WSDL: http://www.webxml.com.cn/zh_cn/w ...
- Bootstrap datepicker可配置网址
http://eternicode.github.io/bootstrap-datepicker/?markup=input&format=&weekStart=&startD ...
- 4个http常用的content type
转的: http://www.aikaiyuan.com/6324.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TR ...
- iis 错误解决汇集
1. Windows7,VS2013,网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“NickLeeCallbackHand ...
- 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列
引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...
- [转] git fetch与pull
原文: http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地 ...


