• C# 基础知识 -- 枚举(Enum)


    枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。

    C# 枚举是值类型。换句话说,枚举包含自己的值,且不能继承或传递继承。

    一、枚举的定义

    枚举列表中的每个符号代表一个整数值, 一个比它前面的符号大的整数值。默认情况下,第一个枚举符号的值是 0。

     1 public enum Days 
     2 {
     3    Monday = 0,
     4    Tuesday,
     5    Wednesday,
     6    Thursday = 10,
     7    Friday,
     8    Saturday,
     9    Sunday
    10 }
    11  
    12 static void Main(string[] args)
    13 {
    14     Console.WriteLine((int)Days.Tuesday);
    15     Console.WriteLine((int)Days.Friday);
    16     Console.ReadLine();
    17 }

    枚举中每个元素的基础类型都是int,可以使用冒号指明另一种整数类型。已批准的枚举类型有:byte、sbyte、short、ushort、int、uint、long、ulong

     1 public enum Days : byte
     2 {
     3     Monday,
     4     Tuesday,
     5     Wednesday,
     6     Thursday,
     7     Friday,
     8     Saturday,
     9     Sunday
    10 }

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

     1 public enum Gender
     2 {
     3     男,
     4  5 }
     6 
     7 static void Main(string[] args)
     8 {
     9     Console.WriteLine("enum ---> int : " + (int)Gender.男);
    10 
    11     Console.WriteLine("enum ---> string : " + Convert.ToString(Gender.男));
    12     Console.WriteLine("enum ---> string : " + Gender.女.ToString());
    13 
    14     Console.WriteLine("int ---> enum : " + (Gender)0);
    15 
    16     Console.WriteLine("int ---> string : " + Enum.GetName(typeof(Gender), 0));
    17 
    18     Console.WriteLine("string ---> enum :" + Enum.Parse(typeof(Gender), ""));
    19 
    20     Console.ReadLine();
    21 }

    三、循环enum

    1 static void Main(string[] args)
    2 {
    3     foreach (var gender in Enum.GetValues(typeof(Gender)))
    4     {
    5         Console.WriteLine(gender.ToString() + "=" + (int)gender);
    6     }
    7     Console.ReadLine();
    8 }

    四、获取enum的描述信息

     1 public enum GenderDesc
     2 {
     3     [Description("male")]
     4     男,
     5     [Description("female")]
     6  7 }
     8 
     9 static void Main(string[] args)
    10 {
    11     Console.WriteLine("女 描述信息 : " + (Description(Gender.女) ?? "null"));
    12     Console.WriteLine("女 描述信息 : " + (Description(GenderDesc.女) ?? "null"));
    13     
    14     Console.ReadLine();
    15 }
    16 
    17 /// <summary>
    18 /// 获取枚举值的描述文本
    19 /// </summary>
    20 /// <param name="e">枚举值</param>
    21 /// <returns></returns>
    22 public static string Description(this Enum e)
    23 {
    24     Type enumType = e.GetType();
    25     var fieldInfo = enumType.GetFields().FirstOrDefault(a => a.Name == Enum.GetName(enumType, e));
    26     object[] obj = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
    27     if (obj == null || obj.Length == 0) return null;
    28 
    29     DescriptionAttribute des = (DescriptionAttribute)obj[0];
    30     return des.Description;
    31 }

     

    五、枚举转换成字典集合的方法

     1 public enum Gender
     2 {
     3     男,
     4  5 }
     6 
     7 static void Main(string[] args)
     8 {
     9     Console.WriteLine("int ---> string : " + EnumHelper.GetEnumName<Gender>(1));
    10 
    11     var dic1 = EnumHelper.getEnumDic<Gender>();
    12     Console.WriteLine("enum ---> dictionary : ");
    13     foreach (var item in dic1)
    14     {
    15         Console.WriteLine(item.Key + "==" + item.Value);
    16     }
    17 
    18     Console.WriteLine("enum ---> dictionary : ");
    19     var dic = EnumHelper.GetDic<Gender>();
    20     foreach (var item in dic)
    21     {
    22         Console.WriteLine(item.Key + "==" + item.Value);
    23     }
    24 
    25     Console.ReadLine();
    26 }
     1 public static class EnumHelper
     2 {
     3     /// <summary>
     4     /// 根据枚举的值获取枚举名称
     5     /// </summary>
     6     /// <typeparam name="T">枚举类型</typeparam>
     7     /// <param name="status">枚举的值</param>
     8     /// <returns></returns>
     9     public static string GetEnumName<T>(this int status)
    10     {
    11         return Enum.GetName(typeof(T), status);
    12     }
    13     /// <summary>
    14     /// 获取枚举名称集合
    15     /// </summary>
    16     /// <typeparam name="T"></typeparam>
    17     /// <returns></returns>
    18     public static string[] GetNamesArr<T>()
    19     {
    20         return Enum.GetNames(typeof(T));
    21     }
    22     /// <summary>
    23     /// 将枚举转换成字典集合
    24     /// </summary>
    25     /// <typeparam name="T">枚举类型</typeparam>
    26     /// <returns></returns>
    27     public static Dictionary<string, int> getEnumDic<T>()
    28     {
    29 
    30         Dictionary<string, int> resultList = new Dictionary<string, int>();
    31         Type type = typeof(T);
    32         var strList = GetNamesArr<T>().ToList();
    33         foreach (string key in strList)
    34         {
    35             string val = Enum.Format(type, Enum.Parse(type, key), "d");
    36             resultList.Add(key, int.Parse(val));
    37         }
    38         return resultList;
    39     }
    40     /// <summary>
    41     /// 将枚举转换成字典
    42     /// </summary>
    43     /// <typeparam name="TEnum"></typeparam>
    44     /// <returns></returns>
    45     public static Dictionary<string, int> GetDic<TEnum>()
    46     {
    47         Dictionary<string, int> dic = new Dictionary<string, int>();
    48         Type t = typeof(TEnum);
    49         var arr = Enum.GetValues(t);
    50         foreach (var item in arr)
    51         {
    52             dic.Add(item.ToString(), (int)item);
    53         }
    54 
    55         return dic;
    56     }
    57 }
    EnumHelper

    五、枚举总结

    枚举变量的使用能避免编码时的HardCode, 有效的提升代码的可读性和可扩展性,当然后期的维护也可以方便很多。

    六、参考文章

    https://www.cnblogs.com/BluceLee/p/8989479.html

  • 相关阅读:
    哪里错了
    任性
    【转】JVM 堆最大能调多大?
    【Android】PA4D_CH7 文件、保存状态和首选项
    【Android】PA4D_CH6 使用Internat资源
    Think In Java 多态学习
    【转】Myeclipse 查看Jar文件里面源码中文乱码 问题解决
    [转载]getBoundClientRect函数详解
    Kettle 环境变量设置
    SQL Server 2012 不支持创建连接服务器到 2000 版本,及其替代方案
  • 原文地址:https://www.cnblogs.com/DerekDeng/p/11613517.html
Copyright © 2020-2023  润新知