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

代码如下:

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. SDK Manager

    dx.bat :将所有的.class文件变成一个.dex文件. aapt:Android Application package tools 安卓应用的打包工具. adb:Android Debug ...

  2. Vue组件的基础用法(火柴)

    前面的话 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用 ...

  3. Python基础点

    写这篇的目的并不是要把python的基础知识汇总一遍,而是着重记录一些实际编写代码时遇到的常用/重要的内容 以点的形式记录,之后遇到的内容会慢慢补充进来 1. 斜杠 / :斜字第一笔, 转义用反斜杠 ...

  4. sqlserver 2017 docker安装(启动代理)

    从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像. docker pull microsoft/mssql-server-linux:2017-latest 运行 ...

  5. python第五天 字典

    今天,已经系统的学习了一下文件相关操作!对三级菜单代码进行的优化: 菜单文件:以字典格式 menu.txt {'第一层':{'第二层':{'第三层':['内容1','内容2','内容3']}},'第一 ...

  6. EasyUI datagrid.getSelections 没有返回正确的选择行数

    Actually i solved the problem. It was because the idField of the table i was using was incorrect. it ...

  7. MySQL参数log_bin_trust_function_creators介绍-存储过程和复制

    MySQL的有个参数log_bin_trust_function_creators,官方文档对这个参数的介绍.解释如下所示: log_bin_trust_function_creators Comma ...

  8. https://www.testingcircus.com/tell-me-about-yourself-6-sample-answers-software-testers/

    https://www.testingcircus.com/tell-me-about-yourself-6-sample-answers-software-testers/ Tell Me Abou ...

  9. 安全之路 —— C/C++实现利用添加注册表项实现文件自启动

    简介 添加注册表项是实现文件自启动的经典方法之一,但因为操作注册表项是一个敏感操作,被报毒可能性较大,但即便如此,这个方法还是值得一学的,因为后期大部分编程都涉及到注册表操作. 最常使用到的注册表项有 ...

  10. Unity RGBA16 + Dither

    游戏开发中有些场合,ETC或者说PVRTC压缩质量不满足的情况下,RGBA32(原图)对美术而言肯定可以满足的,但是RGBA32是不管是对内存占用内存太厉害. RGBA16/RGB16会减少内存的占用 ...