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下 ...
随机推荐
- php服务端写日志文件
1.需求 在服务端记录日志 2.基础版 最基础的文件读写,(要注意window和linux的换行符,window是\r\n,linux是\n),这里就写入一个时间. <?php $handle ...
- Unity调用Android方法
步骤 废话不多说,直接来步骤吧1.创建工程,弄大概像这样一个界面2.在unity中写好代码,像这样,记得给界面绑定好事件啥的 using UnityEngine; using UnityEngine. ...
- C#字符串和数据之间的转换
c#中不仅仅存在数值类型的数据之间的转换,字符串和数值之间也是可以互相转换的,只是方法不同而已. 1 数值型转换为字符型 数值型数据转换为字符串用ToString()方法即可实现 int num1=1 ...
- winkawaks模拟器
Winkawaks (温科沃克斯)win+ka(日文)+wa(也是日文)+k+s 最好的街机模拟器之一,与Nebula和MAME齐名,支持的游戏的有CAPCOM公司的CPS1,CPS2所有游戏,如三国 ...
- (原创)android中使用相机的两种方式
在社交类应用或扫描二维码的场合都需要用到手机上的摄像头 在程序中启用这一硬件主要有两类方法 1.发送intent启动系统自带的摄像应用 此应用的AndroidManifest中的intent-filt ...
- 5.1---二进制数插入(CC150)
public class Solution { public static int binInsert(int n, int m, int i, int j) { // write code here ...
- 修改apache上传文件大小限制
PHP上传文件大小限制解决方法: 第一: 在php.ini里面查看如下行: upload_max_filesize = 8M post_max_size = 10M memory_limi ...
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- Qt不同类之间信号槽连接
1.类必须继承QObject. #ifndef TESTA_H #define TESTA_H #include <QObject> class TestA : public QObjec ...
- Python 输入输出
语法注释 输入输出 #语法缩进,4个空格 #注释 #冒号:结尾,缩进的预计视为代码块 #大小写敏感 #输出 print 300 print 'hello','world' #输入 a=raw_inpu ...


