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

有關(guān)C#枚舉的問答集錦:使用情景

開發(fā) 后端
本文匯總了一些有關(guān)C#枚舉的問答。本文是第三部分,匯總了一些有關(guān)枚舉的使用情景以及一些限制。

之前介紹了C#枚舉的基礎(chǔ)賦值相關(guān)的知識,本文繼續(xù)介紹有關(guān)C#枚舉的一些問答。

Q:我定義了一個這樣的枚舉:

  1. // Code #20  
  2. public enum FontStyle  
  3. {  
  4.     Bold,  
  5.     Italic,  
  6.     Regular,  
  7.     Strikethrough,  
  8.     Underline  
  9. }  

我用它來指定字體的風(fēng)格,但我遇到了麻煩。你知道,字體可以同時擁有枚舉里面所列舉的一種或者多種風(fēng)格,那么,我如何為字體同時指定多種風(fēng)格呢?

A:這個時候你就需要位枚舉(Bit Flags),把Code #20修改一下:

  1. // Code #21  
  2. // I am using the FlagsAttribute to identify a bit flags.  
  3. [Flags]  
  4. public enum FontStyle  
  5. {  
  6.     Bold        = 0x0001,  
  7.     Italic        = 0x0002,  
  8.     Regular        = 0x0004,  
  9.     Strikethrough    = 0x0010,  
  10.     Underline        = 0x0020  
  11. }  

現(xiàn)在,你可以通過按位或運算來為字體指定多種風(fēng)格了:

  1. // Code #22  
  2. // See Code #21 for FontStyle.  
  3. Font f = new Font(  
  4.       FontFamily.GenericSansSerif,  
  5.       12.0F,  
  6.       FontStyle.Italic | FontStyle.Underline  
  7.       );  

--------------------------------------------------------------------------------

Q:位枚舉同樣存在類似于Code #15的惡作劇吧?

A:是的,例如:

  1. // Code #23  
  2. // See Code #21 for FontStyle.  
  3. class Program  
  4. {  
  5.     static void Main()  
  6.     {  
  7.         Bar(FontStyle.Regular | (FontStyle)0x0400);  
  8.     }  
  9.  
  10.     static void Bar(FontStyle fs)  
  11.     {  
  12.         // Code here  
  13.     }  
  14. }  

--------------------------------------------------------------------------------

Q:那么,System.Enum.IsDefine方法是否還能應(yīng)對呢?

A:不能。位枚舉成員并不具備排他性,多個成員可以通過按位或運算組合起來。而System.Enum.IsDefine方法只能判斷枚舉變量的值是否為某一已定義的枚舉成員。請看如下代碼:

  1. // Code #24  
  2. // See Code #21 for FontStyle.  
  3. FontStyle fs1 = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline;  
  4. Console.WriteLine(Enum.IsDefine(typeof(FontStyle), fs1));  
  5.  
  6. FontStyle fs2 = FontStyle.Regular | (FontStyle)0x0400;  
  7. Console.WriteLine(Enum.IsDefine(typeof(FontStyle), fs2));  
  8.  
  9. // Output:  
  10. // false  
  11. // false  

我們對代碼的輸出毫無疑問,因為fs1和fs2都不是一個單獨的枚舉成員。但這不是我們所追求的答案,我們希望區(qū)別對待fs1和fs2,至少我們不希望fs2中的搗蛋家伙——(FontStyle)0x0400——在我們的程序中搞破壞!

--------------------------------------------------------------------------------

Q:那么,我們是否有辦法隔離這些搗蛋鬼呢?

A:Of course!我們同樣可以使用條件判斷語句來處理,但做法將與Code #17和Code #18有所不同。現(xiàn)在我們把Code #23改進如下:

  1. // Code #25  
  2. // See Code #21 for FontStyle.  
  3. class Program  
  4. {  
  5.     static void Main()  
  6.     {  
  7.         Bar(FontStyle.Regular | (FontStyle)0x0400);  
  8.     }  
  9.  
  10.     static void Bar(FontStyle fs)  
  11.     {  
  12.         if ((fs & FontStyle.Bold) != 0)  
  13.         {  
  14.             // Do something associated with bold  
  15.         }  
  16.  
  17.         if ((fs & FontStyle.Italic) != 0)  
  18.         {  
  19.             // Do something associated with italic  
  20.         }  
  21.  
  22.         // Other conditional code continues here  
  23.     }  
  24. }  

我們把枚舉變量與某一特定的位枚舉成員進行按位與運算,若結(jié)果不為0則表明枚舉變量中包含著該位枚舉成員。當(dāng)然,你也可以為自己的代碼寫一組這樣的方法:

  1. // Code #26  
  2. // See Code #21 for FontStyle.  
  3. static bool ContainsBold(FontStyle fs)  
  4. {  
  5.     if ((fs & FontStyle.Bold) != 0)  
  6.         return true;  
  7.     else 
  8.         return false;  
  9. }  
  10.  
  11. static bool ContainsItalic(FontStyle fs)  
  12. {  
  13.     if ((fs & FontStyle.Italic) != 0)  
  14.         return true;  
  15.     else 
  16.         return false;  
  17. }  
  18.  
  19. // Other similar methods continue here  

又或者你可以寫一個這樣的方法:

  1. // Code #27  
  2. // See Code #21 for FontStyle.  
  3. static bool ContainsMember(FontStyle fs, FontStyle menber)  
  4. {  
  5.     if ((fs & member) != 0)  
  6.         return true;  
  7.     else 
  8.         return false;  
  9. }  

如果你只希望判斷某一個枚舉變量里面是否包含搗蛋鬼,你可以寫一個這樣的方法:

  1. // Code #28  
  2. // See Code #21 for FontStyle.  
  3. static bool ContainsPranksters(FontStyle fs)  
  4. {  
  5.     if ((fs < FontStyle.Bold) || (fs > (FontStyle)0x0037))  
  6.         return true;  
  7.  
  8.     if (fs == (FontStyle)0x0008 ||  
  9.         fs == (FontStyle)0x0009 ||  
  10.         fs == (FontStyle)0x0018 ||  
  11.         fs == (FontStyle)0x0019 ||  
  12.         fs == (FontStyle)0x0028 ||  
  13.         fs == (FontStyle)0x0029)  
  14.         return true;  
  15.  
  16.     return false;  
  17. }  

留個“作業(yè)”吧,知道為何這樣可以判斷出是否有搗蛋鬼嗎?當(dāng)然,如果你想到了更好的方法,記住要告訴我喲!

--------------------------------------------------------------------------------

Q:慢著!你那個“我們把枚舉變量與某一特定的位枚舉成員進行按位與運算,若結(jié)果不為0則表明枚舉變量中包含著該位枚舉成員”,在以下的情況顯然不成立的:

  1. // Code #35  
  2. [Flags]  
  3. enum Music  
  4. {  
  5.     Jazz = 0x00,  
  6.     Rock = 0x01,  
  7.     Country = 0x02,  
  8.     Classic = 0x03  
  9. }  
  10.  
  11. static void Main()  
  12. {  
  13.     Music m = Music.Rock | Music.Jazz;  
  14.     int r = (int)(m & Music.Classic);  
  15.     Console.WriteLine(r);  
  16. }  
  17.  

該代碼的輸出恰恰就為0,然而m卻不包含Music.Classic!

A:Good question!也正如你所看到的,這種做法其實與位枚舉成員的值是如何被賦予息息相關(guān)的。那么,我們應(yīng)該如何為位枚舉的成員賦值呢?由于位枚舉成員的值和位運算都直接與二進制相關(guān),所以,我們不妨從二進制的角度去探索一下如何恰當(dāng)?shù)臑槲幻杜e的成員賦值。

試想一下,如果我們能把二進制值的字面特征與位枚舉成員關(guān)聯(lián)起來,使得我們能夠直接從二進制值的字面特征判斷位枚舉成員的存在與否該多好呀!

考察你的Music枚舉,它有4個成員,那么我們把二進制值的數(shù)位設(shè)定為4,即[D][C][B][A]型;并規(guī)定每一個數(shù)位代表該一個枚舉成員,即A代表Music.Jazz、B代表Music.Rock、C代表Music.Country、D代表Music.Classic;那么,某個數(shù)位的值為1就代表其對應(yīng)的枚舉成員存在,為0則不存在?,F(xiàn)在,假如Music的某個變量m的二進制值為0110,我們就可以肯定的說,m中包含著Music.Rock和Music.Country,因其B、C數(shù)位的值均為1。

那么這些跟為位枚舉成員賦值有什么關(guān)系呢?從上面的討論可以知道,Music各個成員的二進制值分別為:Music.Jazz為0001、Music.Rock為0010、Music.Country為0100、Music.Classic為1000。把這些值轉(zhuǎn)換為十六進制值并賦予對應(yīng)的成員:

  1. // Code #36  
  2. [Flags]  
  3. enum Music  
  4. {  
  5.     Jazz = 0x01,  
  6.     Rock = 0x02,  
  7.     Country = 0x04,  
  8.     Classic = 0x08  
  9. }  
  10.  

這樣,你就可以采用我所提到的方法來驗證某個枚舉變量中是否包含著特定的枚舉成員了。

--------------------------------------------------------------------------------

Q:如何把C#枚舉類型轉(zhuǎn)換(解析)成字符串類型?

A:最簡單的方法就是使用System.Enum的

  1. public override string ToString();  

方法,或者把枚舉類型轉(zhuǎn)換為IConvertible接口,再調(diào)用該接口的

  1. string ToString(IFormatProvider provider);  

方法。此時你將得到枚舉成員的字面值的字符串:

  1. // Code #29  
  2. // See Code #01 for Alignment.  
  3. // See Code #21 for FontStyle.  
  4. static void Main()  
  5. {  
  6.     Alignment a = Alignment.Right;  
  7.     Console.WriteLine("Alignment is {0}.", a.ToString());  
  8.  
  9.     FontStyle fs = FontStyle.Bold | FontStyle.Underline;  
  10.     Console.WriteLine("FontStyle is {0}.", fs.ToString());  
  11. }  
  12.  
  13. // Output:  
  14. // Alignment is Right.  
  15. // FontStyle is Bold, Underline.  

如果你希望輸出枚舉成員的值,那么你可以手動指定格式參數(shù):

  1. // Code #30  
  2. // See Code #01 for Alignment.  
  3. // See Code #21 for FontStyle.  
  4. static void Main()  
  5. {  
  6.     Alignment a = Alignment.Right;  
  7.     // Represents Alignment in decimal form.  
  8.     Console.WriteLine("Alignment is {0}.", a.ToString("d"));  
  9.     // Represents Alignment in hexadecimal without a leading "0x".  
  10.     Console.WriteLine("Alignment is {0}.", a.ToString("x"));  
  11.  
  12.     FontStyle fs = FontStyle.Bold | FontStyle.Underline;  
  13.     // Represents FontStyle in decimal form.  
  14.     Console.WriteLine("FontStyle is {0}.", fs.ToString("d"));  
  15.     // Represents FontStyle in hexadecimal without a leading "0x".  
  16.     Console.WriteLine("FontStyle is {0}.", fs.ToString("x"));  
  17. }  
  18.  
  19. // Output:  
  20. // Alignment is 2.  
  21. // Alignment is 00000002.  
  22. // FontStyle is 33.  
  23. // FontStyle is 00000021.  

除此之外,你還可以使用System.Enum的

  1. public static string Format(  
  2.             Type enumType,  
  3.             object value,  
  4.             string format  
  5.             );  
  6.  

方法:

  1. // Code #31  
  2. // See Code #01 for Alignment.  
  3. static void Main()  
  4. {  
  5.     Alignment a = Alignment.Right;  
  6.     Console.WriteLine(  
  7.         "Alignment is 0x{0}.",  
  8.       System.Enum.Format(typeof(Alignment), a, "x")  
  9.         );  
  10.     Console.WriteLine("Alignment is 0x{0:x}.", a);  
  11. }  
  12.  
  13. // Output:  
  14. // Alignment is 0x00000002.  
  15. // Alignment is 0x00000002.  

另外,你還可以通過System.Enum的

  1. public static string[] GetNames(Type enumType);  
  2.  

來獲取枚舉所有成員的字面值:

  1. // Code #32  
  2. // See Code #01 for Alignment.  
  3. static void Main()  
  4. {  
  5.     string[] names = Enum.GetNames(typeof(Alignment));  
  6.     foreach(string name in names)  
  7.         Console.WriteLine(name);  
  8. }  
  9.  
  10. // Output:  
  11. // Left  
  12. // Center  
  13. // Right  

--------------------------------------------------------------------------------

Q:如果我得到一個表示枚舉成員的字符串,我如何將其解析為對應(yīng)枚舉類型呢?

A:這時你就需要System.Enum的

  1. public static object Parse(  
  2.             Type enumType,  
  3.             string value,  
  4.             bool ignoreCase  
  5.             );  
  6.  

方法了:

  1. // Code #33  
  2. // See Code #01 for Alignment.  
  3. // See Code #21 for FontStyle.  
  4. static void Main()  
  5. {  
  6.     string name = "Right";  
  7.     Alignment a = (Alignment)Enum.Parse(typeof(Alignment), name, false);  
  8.  
  9.     Console.WriteLine(a.ToString());  
  10.  
  11.     string names = "Bold, Italic, Underline";  
  12.     FontStyle fs = (FontStyle)Enum.Parse(typeof(FontStyle), names, false);  
  13.  
  14.     Console.WriteLine(fs.ToString());  
  15. }  
  16.  
  17. // Output:  
  18. // Right  
  19. // Bold, Italic, Underline  

--------------------------------------------------------------------------------

Q:枚舉類型為我們編碼提供了巨大的便利,什么情況下我們不應(yīng)該使用枚舉呢?

A:首先你應(yīng)該清楚枚舉類型在編程中充當(dāng)一個什么樣的角色。在我看來,枚舉類型表達了一種穩(wěn)定的分類標(biāo)準(zhǔn)。當(dāng)你查看.NET Framework BCL中的枚舉類型,你會發(fā)現(xiàn)它們幾乎沒有任何改變的可能或者趨勢,表現(xiàn)出一種穩(wěn)定性。所以,當(dāng)你所要表達的分類標(biāo)準(zhǔn)也同樣具備這種穩(wěn)定性時,你就可以考慮枚舉類型了。那么什么情況下不使用枚舉呢?一般說來,當(dāng)分類標(biāo)準(zhǔn)不閉合時——即新的子分類隨時有可能產(chǎn)生或者現(xiàn)有子分類隨時有可能被替換——你就應(yīng)該考慮使用其他的方式來表達了。

下面讓我們來看一個薪酬自動管理系統(tǒng)的一部分,假設(shè)某公司現(xiàn)有雇員種類為:

1. Programmer
2. Salesman
3. Manager

相關(guān)代碼如下:

  1. // Code #34  
  2. public enum EmployeeKind  
  3. {  
  4.     Programmer,  
  5.     Salesman,  
  6.     Manager  
  7. }  
  8.  
  9. public class Employee  
  10. {  
  11.     public Employee(string name, EmployeeKind kind)  
  12.     {  
  13.         Kind = kind;  
  14.     }  
  15.  
  16.     private string m_Name;  
  17.     public string Name  
  18.     {  
  19.         get { return m_Name; }  
  20.     }  
  21.  
  22.     public readonly EmployeeKind Kind;  
  23.  
  24.     public double GetPayment()  
  25.     {  
  26.         switch(Kind)  
  27.         {  
  28.             case EmployeeKind.Programmer:  
  29.                 // Return payment  
  30.             case EmployeeKind.Salesman:  
  31.                 // Return payment  
  32.             case EmployeeKind.Manager:  
  33.                 // Return payment  
  34.         }  
  35.     }  
  36. }  

假如該公司正處于成長期,那么公司的組織結(jié)構(gòu)發(fā)生改變是家常便飯之事。但公司每一次改變組織結(jié)構(gòu),這樣的系統(tǒng)就要經(jīng)歷源代碼修改、重新編譯然后再部署的過程!而且,如果公司實行了新的績效評估方案,并把薪酬計算與績效追蹤掛鉤,那么GetPayment方法將進一步壯大,以至于最終其代碼晦澀難懂。你當(dāng)然可以把GetPayment分割為子方法,但并沒有什么實質(zhì)的改變。再想一想,如果將來公司打算把薪酬系統(tǒng)與銀行掛鉤,提供薪資直接銀行劃撥,那將又會是怎么一番局面呢?

以上就總結(jié)了一些C#枚舉應(yīng)用情景的相關(guān)問答。

【編輯推薦】

  1. 有關(guān)C#枚舉的問答集錦:有關(guān)賦值
  2. 有關(guān)C#枚舉的問答集錦:基礎(chǔ)篇
  3. C#枚舉和數(shù)學(xué)習(xí)經(jīng)驗總結(jié)
  4. 淺談如何利用C#枚舉所有的窗體
  5. C#記憶功能的地址欄控件
責(zé)任編輯:yangsai 來源: CSDN博客
相關(guān)推薦

2009-08-11 14:55:44

C#枚舉

2009-08-11 14:44:24

C#枚舉

2010-08-12 18:01:38

ibmdwJazz

2010-12-29 09:51:06

配置vsftpdDebian

2012-07-05 09:42:08

jQuery

2009-08-18 09:37:14

C#枚舉類型

2010-03-12 09:47:22

2009-08-18 10:30:30

C#枚舉

2011-03-03 13:25:57

2013-06-21 10:21:30

桌面虛擬化案例

2010-01-18 16:14:43

配置VLAN交換機

2009-08-18 13:06:17

C#枚舉類型

2009-08-17 18:15:23

C# 枚舉使用

2009-12-29 14:41:12

ADSL常見硬件問題

2009-08-17 18:31:39

C# 枚舉

2009-08-18 13:35:06

C#枚舉文件

2009-08-17 17:49:20

C# 枚舉

2009-08-18 10:17:25

C#枚舉類型

2009-08-18 10:47:40

C#枚舉類型

2009-08-17 17:56:32

C# 枚舉
點贊
收藏

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