C#枚舉類型使用的一點(diǎn)總結(jié)
作者:佚名
C#枚舉類型使用的一點(diǎn)總結(jié)主要是向你講解C#枚舉類型使用過(guò)程中的一點(diǎn)體會(huì)和知識(shí)點(diǎn)的強(qiáng)調(diào),希望對(duì)你學(xué)習(xí)C#枚舉類型有所幫助。
C#枚舉類型使用的時(shí)候需要掌握的有哪些方面呢?首先我們來(lái)看看一個(gè)例子:
- public enum Colors { Red = 1, Green = 2,
- Blue = 4, Yellow = 8 };
- The entries of the Colors Enum are:
- Red
- Green
- Blue
- Yellow
C#枚舉類型使用的一點(diǎn)總結(jié)之根據(jù)name獲得Enum的類型:
- Colors mycolor = (Colors)Enum.Parse(
- typeof(Colors),"red",true);
- (int)mycolor1=1
- mycolor1.GetTypeCode=Int32
C#枚舉類型使用的一點(diǎn)總結(jié)之根據(jù)value獲得Enum的類型:
- Colors mycolor = (Colors)Enum.Parse(
- typeof(Colors),"1",true);
- mycolor2.ToString()=Red
- mycolor2.GetTypeCode=Int32
C#枚舉類型使用的一點(diǎn)總結(jié)之遍歷枚舉內(nèi)容
- foreach(string s in Enum.GetNames(typeof(Colors)))
- {
- //to do
- }
- Colors myOrange = (Colors)Enum.Parse(
- typeof(Colors), "Red, Blue,Yellow");
- The myOrange value has the combined
- entries of [myOrange.ToString()]=13
- Colors myOrange2 = (Colors)Enum.Parse(
- typeof(Colors), "Red, Blue");
- The myOrange2 value has the combined
- entries of [myOrange2.ToString()]=5
C#枚舉類型使用的一點(diǎn)總結(jié)的內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#枚舉類型有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來(lái)源:
tz8.net