日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
有關(guān)C#枚舉的問答集錦:使用情景

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

為正寧等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及正寧網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、正寧網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

Q:我定義了一個(gè)這樣的枚舉:

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

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

A:這個(gè)時(shí)候你就需要位枚舉(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)在,你可以通過按位或運(yù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:不能。位枚舉成員并不具備排他性,多個(gè)成員可以通過按位或運(yùn)算組合起來。而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  

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

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

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

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

 
 
 
  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. }  

我們把枚舉變量與某一特定的位枚舉成員進(jìn)行按位與運(yùn)算,若結(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  

又或者你可以寫一個(gè)這樣的方法:

 
 
 
  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. }  

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

 
 
 
  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. }  

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

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

Q:慢著!你那個(gè)“我們把枚舉變量與某一特定的位枚舉成員進(jìn)行按位與運(yùn)算,若結(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!也正如你所看到的,這種做法其實(shí)與位枚舉成員的值是如何被賦予息息相關(guān)的。那么,我們應(yīng)該如何為位枚舉的成員賦值呢?由于位枚舉成員的值和位運(yùn)算都直接與二進(jìn)制相關(guān),所以,我們不妨從二進(jìn)制的角度去探索一下如何恰當(dāng)?shù)臑槲幻杜e的成員賦值。

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

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

那么這些跟為位枚舉成員賦值有什么關(guān)系呢?從上面的討論可以知道,Music各個(gè)成員的二進(jìn)制值分別為:Music.Jazz為0001、Music.Rock為0010、Music.Country為0100、Music.Classic為1000。把這些值轉(zhuǎn)換為十六進(jì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.  

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

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

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

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

 
 
 
  1. public override string ToString();  

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

 
 
 
  1. string ToString(IFormatProvider provider);  

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

 
 
 
  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.  

如果你希望輸出枚舉成員的值,那么你可以手動(dòng)指定格式參數(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:如果我得到一個(gè)表示枚舉成員的字符串,我如何將其解析為對應(yīng)枚舉類型呢?

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

下面讓我們來看一個(gè)薪酬自動(dò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)歷源代碼修改、重新編譯然后再部署的過程!而且,如果公司實(shí)行了新的績效評估方案,并把薪酬計(jì)算與績效追蹤掛鉤,那么GetPayment方法將進(jìn)一步壯大,以至于最終其代碼晦澀難懂。你當(dāng)然可以把GetPayment分割為子方法,但并沒有什么實(shí)質(zhì)的改變。再想一想,如果將來公司打算把薪酬系統(tǒng)與銀行掛鉤,提供薪資直接銀行劃撥,那將又會(huì)是怎么一番局面呢?

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


新聞名稱:有關(guān)C#枚舉的問答集錦:使用情景
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/dpoghic.html