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

代码如下:

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. 给 Linux 系统“减肥”,系统垃圾清理_系统安装与配置管理_Linux Today - Google Chrome

    给 Linux 系统"减肥",系统垃圾清理  2013/10/16  linux  系统安装与配置管理  评论  15,555 Linux 计算机安装后,在我们不断的使用过程中,因 ...

  2. XP环境下C# 调用Pocess.start()时提示文件找不到的错误解决办法

    错误提示如下: System.ComponentModel.Win32Exception (0x80004005): 系统找不到指定的文件. 在 System.Diagnostics.Process. ...

  3. 关于使用WeUI在IE中提示“font-face 未能完成 OpenType 嵌入权限检查。权限必须是可安装的。”的问题

    @font-face是css3中定义字体的规则. 首先,在使用weui时,在Chrome.Firefox下没有问题,但是在IE下提示“font-face 未能完成 OpenType 嵌入权限检查.权限 ...

  4. EntityFramework Code-First 简易教程(十)-------多对多

    配置Many-to-Many(多对多)关系: 这里有两个类,Student和Course,一个Student可以有多个Course,一个Course也可以有多个Student,所以这就成了多对多关系. ...

  5. 关于MySQL checkpoint

    Ⅰ.Checkpoint 1.1 checkpoint的作用 缩短数据库的恢复时间 缓冲池不够用时,将脏页刷到磁盘 重做日志不可用时,刷新脏页 1.2 展开分析 page被缓存在bp中,page在bp ...

  6. 用Python实现数据结构之树

    树 树是由根结点和若干颗子树构成的.树是由一个集合以及在该集合上定义的一种关系构成的.集合中的元素称为树的结点,所定义的关系称为父子关系.父子关系在树的结点之间建立了一个层次结构.在这种层次结构中有一 ...

  7. Java jni字符串转换

    1.jstring转QString 对于Qt5.2以上(含)可以用QAndroidJniObject::toString(),详见这里:https://stackoverflow.com/questi ...

  8. Linux 小知识翻译 - 「Linux之父 Linus」

    作为新年的第一次,这次想简单介绍下Linus这个人.(这篇文章是作者新年初写的,所以有这么句话) Linux之父,同时也是现在linux内核开发最终决定的人物就是「Linus Torvalds」.「L ...

  9. 【项目 · WonderLand】 系 统 设 计

    团 队 作 业 ---- 系 统 设 计 Part 0 · 简 要 目 录 Part 1 · 完 善 需 求 规 格 说 明 书 Part 2 · 团 队 编 码 规 范 Part 3 · 数 据 库 ...

  10. Beta冲刺(4/5)(麻瓜制造者)

    今日已完成 邓弘立:完成了商品管理(下架)和搜索功能 符天愉:完成了后台管理员界面的登录和其他视图的载入 江郑:昨天来决定跨域执行请求,后台参考一些意见以后,操作起来没有那么容易实现,和队友交流以后本 ...