C# 枚举基本用法及扩展方法

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

代码如下:

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、枚举的...

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

谢谢!

爱生活爱学习

posted @ 2017-07-11 15:24  天才卧龙  阅读(1026)  评论(0编辑  收藏  举报