没什么好说的,都是些基础!

代码如下:

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), );//true
Console.WriteLine(bol);
bol = Enum.IsDefined(typeof(Pepole), );//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 > )
return attributes[].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 = , [Description("女孩")]
girl = , NoSex= }
}

以上只是基础用法,关于枚举在什么情景下用,这才是重要的。

下面就探讨下我的理解吧

如下情况:

1、数据库中存在‘标识’字段,比如:Sex字段,有 男、女、暂未填写性别 之分,如果在C#程序中采用硬编码的方式写程序,就会出现如下情况:

查询所有男生:Select * from Person where Sex=1

查询所有女生:Select * from Person where Sex=2

等等

试想:如果将这种硬编码写在项目中多处,那么如果将来需求有变化,Sex为1是表示女,在这种情况下,你必须一处一处修改你的代码。

但是,如果你采用了枚举整体编码,那么我们只需修改枚举一处即可。

2、枚举的读取速度是相当快的,在某种程度上,也会加快程序的执行效率。

3、枚举的...

不管怎么样吧,存在即有意义,如果在可以用枚举统一管理的情况下,建议采用

谢谢!

爱生活爱学习

C# 枚举基本用法及扩展方法的更多相关文章

  1. 枚举扩展方法获取枚举Description

    枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...

  2. 枚举转SelectList扩展方法

        public enum Avbc    {        Red=1,        Blue=2,        Whilt=3,        Black=4    } public st ...

  3. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  4. C# 扩展方法——获得枚举的Description

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html /// <summary> /// 扩展方法,获得枚举的Descrip ...

  5. Java enum(枚举)的用法详解(转)

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...

  6. C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询

    1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...

  7. Java Enum枚举的用法(转)

    说明:Java的枚举比dotnet的枚举好用,至少支持的方式有很多. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以 ...

  8. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  9. C#的扩展方法解析

    在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...

随机推荐

  1. Java学习笔记:输入、输出数据

    相关内容: 输出数据: print println printf 输入数据: Scanner 首发时间:2018-03-16 16:30 输出数据: JAVA中在屏幕中打印数据可以使用: System ...

  2. The stacking context

    文档中的层叠上下文由满足以下任意一个条件的元素形成: 1. z-index 值不为 "auto"的 绝对/相对定位. 2. position位fixed. 3. opacity 属 ...

  3. maven(二):创建一个可用的maven项目,完整过程

    环境:eclipse4.5 (内置maven插件) 创建maven项目 文件菜单--新建--其他-- maven project 下一步 选择web 结构 group id:  指项目在maven本地 ...

  4. 第一章 Hyper-V 2012 R2角色部署

      在windows server 2012 R2中,我们可以通过安装hyper-v角色来完成虚拟化底层架构的部署.除了图形界面的安装,也可以使用单独的发行版Hyper-V Server 2012 R ...

  5. Spring RestTemplate 中文乱码问题

    1.原因 由于RestTemplate的默认构造方法初始化的StringHttpMessageConverter的默认字符集是ISO-8859-1,所以导致RestTemplate请求的响应内容会出现 ...

  6. 【17】有关python面向对象编程的提高【多继承、多态、类属性、动态添加与限制添加属性与方法、@property】

    一.多继承 案例1:小孩继承自爸爸,妈妈.在程序入口模块再创建实例调用执行 #father模块 class Father(object): def __init__(self,money): self ...

  7. Django templates 模板的语法

    MVC 以及 MTV MVC: M : model -->> 存取数据(模型) V: view -->> 信息的展示(视图) C: controller -->> ...

  8. BZOJ4170:极光(CDQ分治)

    Description "若是万一琪露诺(俗称rhl)进行攻击,什么都好,冷静地回答她的问题来吸引她.对方表现出兴趣的话,那就慢慢地反问.在她考虑答案的时候,趁机逃吧.就算是很简单的问题,她 ...

  9. 转://SIHA环境修改主机名实施步骤

    目 录1 实施需求 2 修改主机名 2.1 停止HAS服务 2.2 修改主机名 3 重新配置服务 3.1 使用root用户重新配置CSS & OHAS服务 3.2 设置cssd自动启动属性 3 ...

  10. windows下安装ElasticSearch 5

    ElasticSearch简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用 ...