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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM