c#枚举使用详解

简介

1. 枚举(enum type)通常用来表示一组常量。由于枚举是强类型的,这在编程中给我们提供了极大的方便。

2. 枚举的定义:

 public enum Sex
        {
            男 = 0,
            女 = 1
        }

或者:如果只给男赋值,那么女=1

 public enum Sex
        {
            男 = 0,
            女 
        }

 

枚举在软件开发中的使用场景

在数据库设计人员表(person)时有性别字段Sex(0代表男,1代表女),我们一般用bit或者int类型表示。

1.在编程时我们给Sex字段赋值的方式为:

1).  Sex=0;

2).  Sex=(int)SexEnum.Man;

其中SexEnum为定义性别的枚举类型,我们可以看出第二种方式的可读性更强。

2.在编程时我们,如果Sex字段作为一个搜索条件的话,我们可能需要以下拉选择的方式展现所有可以选择的情况。那么我们就需要将SexEnum转换成一个字典集合然后绑定到对应的select标签,具体怎么实现请看下面的示例代码。

………………………………

enum、int、string三种类型之间的互转

执行结果如下:

获取描述信息

 修改枚举如下:

获取描述信息代码如下:

打印结果如下:

 

枚举转换成字典集合的通用方法

1.这里我就直接列举代码如下:

 public static class EnumHelper
    {
        /// <summary>
        /// 根据枚举的值获取枚举名称
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="status">枚举的值</param>
        /// <returns></returns>
        public static string GetEnumName<T>(this int status)
        {
            return Enum.GetName(typeof(T), status);
        }
        /// <summary>
        /// 获取枚举名称集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string[] GetNamesArr<T>()
        {
            return Enum.GetNames(typeof(T));
        }
        /// <summary>
        /// 将枚举转换成字典集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> getEnumDic<T>()
        {

            Dictionary<string, int> resultList = new Dictionary<string, int>();
            Type type = typeof(T);
            var strList = GetNamesArr<T>().ToList();
            foreach (string key in strList)
            {
                string val = Enum.Format(type, Enum.Parse(type, key), "d");
                resultList.Add(key, int.Parse(val));
            }
            return resultList;
        }
        /// <summary>
        /// 将枚举转换成字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> GetDic<TEnum>()
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            Type t = typeof(TEnum);
            var arr = Enum.GetValues(t);
            foreach (var item in arr)
            {
                dic.Add(item.ToString(), (int)item);
            }

            return dic;
        }

    }
  public enum Sex
    {
        man,
        woman
    }
    public enum Color
    {
        red,
        blue
    }

 

使用方法如下:

 static void Main(string[] args)
        {
            var name = EnumHelper.GetEnumName<Sex>(1);
            Console.WriteLine("数字转字符串:"+name);
            var dic1 = EnumHelper.getEnumDic<Sex>();
            Console.WriteLine("枚举转字典集合方法1:");
            foreach (var item in dic1)
            {
                Console.WriteLine(item.Key + "==" + item.Value);
            }
            Console.WriteLine("枚举转字典集合方法2:");
            var dic= EnumHelper.GetDic<Color>();
            foreach (var item in dic)
            {
                Console.WriteLine(item.Key+"=="+item.Value);
            }
            Console.ReadLine();
        }

 
posted @ 2016-10-11 17:31  梦亦晓  阅读(44764)  评论(0编辑  收藏  举报