自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
C# 枚舉和常量應(yīng)用區(qū)別是什么呢?C# 枚舉和常量應(yīng)用起來有什么實(shí)際區(qū)別呢?那么本文就向你介紹這方面的內(nèi)容。

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

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

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

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

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﹤intstring﹥ 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﹤intstring﹥();  
  20. list.Add(Morning, "上午");  
  21. list.Add(Afternoon, "下午");  
  22. list.Add(Evening, "晚上");  
  23. }  
  24.  
  25. return list[time];  
  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. /// 從枚舉類型和它的特性讀出并返回一個(gè)鍵值對  
  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. nullnull)).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;  

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

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

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

【編輯推薦】

  1. C# listview進(jìn)度條顯示淺析
  2. C# 進(jìn)度條效果實(shí)現(xiàn)實(shí)例
  3. C# 枚舉簡介及優(yōu)點(diǎn)淺析
  4. C# 枚舉實(shí)例應(yīng)用淺析
  5. C# 枚舉常用方法淺析
責(zé)任編輯:仲衡 來源: 博客園
相關(guān)推薦

2009-08-18 10:35:46

C#枚舉類型

2009-08-17 17:49:20

C# 枚舉

2009-08-18 10:30:30

C#枚舉

2009-08-18 09:51:18

C#枚舉類型

2009-08-17 17:56:32

C# 枚舉

2009-08-18 12:52:33

C#枚舉類型

2009-08-17 17:36:08

C# 枚舉

2009-08-18 09:37:14

C#枚舉類型

2009-08-18 13:00:59

C#枚舉類型

2009-08-18 10:41:38

C#枚舉類型

2021-08-04 08:33:59

TypeScriptConst Readonly

2009-08-13 14:06:37

C#結(jié)構(gòu)體結(jié)構(gòu)體和類的區(qū)別

2009-08-07 08:53:52

C# ICloneab

2009-09-07 04:19:56

C#窗體事件

2009-08-27 14:12:02

C# interfac

2009-08-21 11:31:59

異步和多線程的區(qū)別

2009-08-19 10:41:14

C# switch和c

2009-08-27 13:50:08

C# StringBu

2009-08-24 15:12:13

C# 泛型接口

2009-08-13 17:30:30

C#構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號