日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#枚舉和常量應(yīng)用區(qū)別淺析

C# 枚舉和常量應(yīng)用區(qū)別是什么呢?

當我們需要定義的時候呢,優(yōu)先考慮枚舉。

在C#中,枚舉的真正強大之處是它們在后臺會實例化為派生于基類System.Enum的結(jié)構(gòu)。這表示可以對它們調(diào)用方法,執(zhí)行有用的任務(wù)。注意因為.NET Framework的執(zhí)行方式,在語法上把枚舉當做結(jié)構(gòu)是不會有性能損失的。實際上,一旦代碼編譯好,枚舉就成為基本類型,與int和float類似。

但是在實際應(yīng)用中,你也許會發(fā)現(xiàn),我們經(jīng)常用英語定義枚舉類型,因為開發(fā)工具本來就是英文開發(fā)的,美國人用起來,就直接能夠明白枚舉類型的含義。其實,我們在開發(fā)的時候就多了一步操作,需要對枚舉類型進行翻譯。沒辦法,誰讓編程語言是英語寫的,如果是漢語寫的,那我們也就不用翻譯了,用起枚舉變得很方便了。舉個簡單的例子,TimeOfDay.Morning一看到Morning,美國人就知道是上午,但是對于中國的使用者來說,可能有很多人就看不懂,這就需要我們進行翻譯、解釋,就向上面的getTimeOfDay()的方法,其實就是做了翻譯工作。所以,在使用枚舉的時候,感覺到并不是很方便,有的時候我們還是比較樂意創(chuàng)建常量,然后在類中,聲明一個集合來容納常量和其意義。

C# 枚舉和常量之使用常量定義:這種方法固然可行,但是不能保證傳入的參數(shù)day就是實際限定的。

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3.  //C# 枚舉和常量應(yīng)用區(qū)別
  4. public class TimesOfDay
  5. {
  6. public const int Morning = 0;
  7. public const int Afternoon = 1;
  8. public const int Evening = 2;
  9. public static Dictionary﹤int, string﹥ list;
  10. /// ﹤summary﹥
  11. /// 獲得星期幾
  12. /// ﹤/summary﹥
  13. /// ﹤param name="day"﹥﹤/param﹥
  14. /// ﹤returns﹥﹤/returns﹥
  15. public static string getTimeNameOfDay(int time)
  16. {
  17. if (list == null || list.Count ﹤= 0)
  18. {
  19. list = new Dictionary﹤int, string﹥();
  20. list.Add(Morning, "上午");
  21. list.Add(Afternoon, "下午");
  22. list.Add(Evening, "晚上");
  23. }
  24. return list[time];
  25. }
  26. }

希望能夠找到一種比較好的方法,將枚舉轉(zhuǎn)為我們想要的集合。搜尋了半天終于找到了一些線索。通過反射,得到針對某一枚舉類型的描述。

C# 枚舉和常量應(yīng)用區(qū)別之枚舉的定義中加入描述

 
 
 
  1. using System;
  2. using System.ComponentModel;
  3.  //C# 枚舉和常量應(yīng)用區(qū)別
  4. public enum TimeOfDay
  5. {
  6. [Description("上午")]
  7. Moning,
  8. [Description("下午")]
  9. Afternoon,
  10. [Description("晚上")]
  11. Evening,
  12. };

C# 枚舉和常量應(yīng)用區(qū)別之獲得值和表述的鍵值對

 
 
 
  1. /// ﹤summary﹥
  2. /// 從枚舉類型和它的特性讀出并返回一個鍵值對
  3. /// ﹤/summary﹥
  4. /// ﹤param name="enumType"﹥
  5. Type,該參數(shù)的格式為typeof(需要讀的枚舉類型)
  6. ﹤/param﹥
  7. /// ﹤returns﹥鍵值對﹤/returns﹥
  8. public static NameValueCollection 
  9. GetNVCFromEnumValue(Type enumType)
  10. {
  11. NameValueCollection nvc = new NameValueCollection();
  12. Type typeDescription = typeof(DescriptionAttribute);
  13. System.Reflection.FieldInfo[] 
  14. fields = enumType.GetFields();
  15. string strText = string.Empty;
  16. string strValue = string.Empty;
  17. foreach (FieldInfo field in fields)
  18. {
  19. if (field.FieldType.IsEnum)
  20. {
  21. strValue = ((int)enumType.InvokeMember(
  22. field.Name, BindingFlags.GetField, null, 
  23. null, null)).ToString();
  24. object[] arr = field.GetCustomAttributes(
  25. typeDescription, true);
  26. if (arr.Length ﹥ 0)
  27. {
  28. DescriptionAttribute aa = 
  29. (DescriptionAttribute)arr[0];
  30. strText = aa.Description;
  31. }
  32. else
  33. {
  34. strText = field.Name;
  35. }
  36. nvc.Add(strText, strValue);
  37. }
  38. }  //C# 枚舉和常量應(yīng)用區(qū)別
  39. return nvc;
  40. }

當然,枚舉定義的也可以是中文,很簡單的解決的上面的問題,但是,我們的代碼看起來就不是統(tǒng)一的語言了。

 
 
 
  1. ChineseEnum
  2. public enum TimeOfDay
  3. {
  4. 上午,
  5. 下午,
  6. 晚上,
  7. }

C# 枚舉和常量應(yīng)用區(qū)別的基本情況就向你介紹到這里,希望對你了解和學習C# 枚舉有所幫助。


分享文章:C#枚舉和常量應(yīng)用區(qū)別淺析
鏈接分享:http://m.5511xx.com/article/cdgiije.html