public enum Seasons
{
Spring,
Summer,
Autumn,
Winner
}
void TestEnum(){
Seasons s =Seasons.Winner;
Console.WriteLine(s);
Console.ReadKey();
}
1)将枚举类型强转成int类型
Seasons season = Seasons.Summer;
int n = (int) season;
Console.WriteLine(n);
Console.WriteLine((int)Seasons.Autumn);
2)将int类型强转为枚举类型
int n1 = 3;
Seasons season = (Seasons) n1 ;
Console.WriteLine(season);
3)将枚举类型转换成字符串类型
//所有的类型都能够转换成string类型
Seasons season = Seasons.Spring;
string s = season.ToString();
Console.WriteLine(s);
4)将字符串类型转换为枚举类型
string s = "aaa";
Seasons season = (Seasons)Enum.Parse(typeof(Seasons),s);
//Convert.ToInt32();
int.Parse() ; int.TryParse();
Console.WriteLine(season);
public enum QQState
{
OnLine=1,
OffLine,
Leave,
Busy,
QMe
}
public void Test(){
//枚举练习
//提示用户选择一个在线状态,我们接受,并将用户的输入转换成枚举类型。
//再次打印到控制台中
Console.WriteLine("请选择您的qq在线状态 1--OnLine 2--OffLine 3--Leave 4--Busy
5--QMe");
string input = Console.ReadLine();
switch (input)
{
case "1": QQState s1 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择的在线状态是{0}",s1);
break;
case "2": QQState s2 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择的在线状态是{0}",s2);
break;
case "3": QQState s3 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择的在线状态是{0}",s3);
break;
case "4": QQState s4 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择的在线状态是{0}", s4);
break;
case "5": QQState s5 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择的在线状态是{0}", s5);
break;
}
Console.ReadKey();
}