C# 枚举基本用法及扩展方法
2017-07-11 15:24
369 查看
没什么好说的,都是些基础!
代码如下:
View Code
以上只是基础用法,关于枚举在什么情景下用,这才是重要的。
下面就探讨下我的理解吧
如下情况:
1、数据库中存在‘标识’字段,比如:Sex字段,有 男、女、暂未填写性别 之分,如果在C#程序中采用硬编码的方式写程序,就会出现如下情况:
查询所有男生:Select * from Person where Sex=1
查询所有女生:Select * from Person where Sex=2
等等
试想:如果将这种硬编码写在项目中多处,那么如果将来需求有变化,Sex为1是表示女,在这种情况下,你必须一处一处修改你的代码。
但是,如果你采用了枚举整体编码,那么我们只需修改枚举一处即可。
2、枚举的读取速度是相当快的,在某种程度上,也会加快程序的执行效率。
3、枚举的...
不管怎么样吧,存在即有意义,如果在可以用枚举统一管理的情况下,建议采用
谢谢!
爱生活爱学习
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace DFS { class Program { static void Main(string[] args) { string boy = GetDescription(Pepole.boy); Console.WriteLine(boy); string Girl = GetDescription(Pepole.girl, true); Console.WriteLine(Girl); string NoSex = GetDescription(Pepole.NoSex, true); Console.WriteLine(NoSex); //枚举自带方法: Array PersonAry = Enum.GetValues(typeof(Pepole));//"Array 类是支持数组的语言实现的基类。但是,只有系统和编译器能够从 Array 类显式派生。用户应当使用由语言提供的数组构造。" foreach (var item in PersonAry) { Console.WriteLine("GetValues结果如下:" + item); } string MyGirl = Enum.GetName(typeof(Pepole),Pepole.girl); Console.WriteLine("我的姑娘如下:"+MyGirl); string[] strAry = new string[] { }; strAry = Enum.GetNames(typeof(Pepole)); foreach (var item in strAry) { Console.WriteLine("GetNames结果如下:" + item); } bool bol = Enum.IsDefined(typeof(Pepole), 1);//true Console.WriteLine(bol); bol = Enum.IsDefined(typeof(Pepole), 33);//false Console.WriteLine(bol); bol = Enum.IsDefined(typeof(Pepole), "boy");//true Console.WriteLine(bol); bol = Enum.IsDefined(typeof(Pepole), "男孩");//false Console.WriteLine(bol); Console.ReadKey(); } /// <summary> /// 操作枚举类 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string GetDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="value">枚举值</param> /// <param name="nameInstend">当枚举没有定义DescriptionAttribute,是否用枚举名代替,默认使用</param> /// <returns>枚举的Description</returns> public static string GetDescription(Enum value, bool nameInstend = true) { Type type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } FieldInfo field = type.GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstend == true) { return name; } return attribute == null ? null : attribute.Description; } } public enum Pepole { [Description("男孩")] boy = 1, [Description("女孩")] girl = 2, NoSex=3 } }
View Code
以上只是基础用法,关于枚举在什么情景下用,这才是重要的。
下面就探讨下我的理解吧
如下情况:
1、数据库中存在‘标识’字段,比如:Sex字段,有 男、女、暂未填写性别 之分,如果在C#程序中采用硬编码的方式写程序,就会出现如下情况:
查询所有男生:Select * from Person where Sex=1
查询所有女生:Select * from Person where Sex=2
等等
试想:如果将这种硬编码写在项目中多处,那么如果将来需求有变化,Sex为1是表示女,在这种情况下,你必须一处一处修改你的代码。
但是,如果你采用了枚举整体编码,那么我们只需修改枚举一处即可。
2、枚举的读取速度是相当快的,在某种程度上,也会加快程序的执行效率。
3、枚举的...
不管怎么样吧,存在即有意义,如果在可以用枚举统一管理的情况下,建议采用
谢谢!
爱生活爱学习
相关文章推荐
- C#:USB设备枚举(七)WPF扩展方法
- C#中this用法系列(二) 通过this修饰符为原始类型扩展方法
- [原创]扩展方法基本用法
- c#枚举的用法及遍历方法
- C#知识点总结(静态类,扩展方法,TryParse用法)
- C#+Enum 枚举扩展方法获取name,value和Description
- c#枚举(Enum)的用法及遍历方法
- c#枚举(Enum)的用法及遍历方法
- [深入学习C#]LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询
- 关于C#中this的一个用法----用于C#原始类型扩展方法
- C#中this用法系列(二) 通过this修饰符为原始类型扩展方法
- C#:USB设备枚举(七)WPF扩展方法
- C# 3.0新特性之扩展方法
- C# 3.0 新特性:扩展方法初探
- C# 枚举的获取方法
- Linq的那些事——从Linq扩展方法回顾C#语言基础
- C# 3.0 扩展方法 实践
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C#2008新特性--(扩展方法)Extension Method
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)