av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C#枚舉和常量應(yīng)用區(qū)別淺析

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

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

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

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

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

 
 
 
  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)為我們想要的集合。搜尋了半天終于找到了一些線索。通過(guò)反射,得到針對(duì)某一枚舉類型的描述。

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ū)別之獲得值和表述的鍵值對(duì)

 
 
 
  1. /// ﹤summary﹥
  2. /// 從枚舉類型和它的特性讀出并返回一個(gè)鍵值對(duì)
  3. /// ﹤/summary﹥
  4. /// ﹤param name="enumType"﹥
  5. Type,該參數(shù)的格式為typeof(需要讀的枚舉類型)
  6. ﹤/param﹥
  7. /// ﹤returns﹥鍵值對(duì)﹤/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. }

當(dāng)然,枚舉定義的也可以是中文,很簡(jiǎn)單的解決的上面的問(wèn)題,但是,我們的代碼看起來(lái)就不是統(tǒng)一的語(yǔ)言了。

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

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


分享文章:C#枚舉和常量應(yīng)用區(qū)別淺析
標(biāo)題URL:http://uogjgqi.cn/article/cdgiije.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流