标签:
public enum QQState
{
OnLine=1,
OffLine,
Leave,
Busy,
QMe
}
枚举转int:
QQState state = QQState.OnLine;
//枚举类型默认可以跟int类型互相转换 枚举类型跟int类型是兼容的
int n = (int)state;
Console.WriteLine(n);
Console.WriteLine((int)QQState.OffLine);
Console.WriteLine((int)QQState.Leave);
Console.WriteLine((int)QQState.Busy);
Console.WriteLine((int)QQState.QMe);
Console.ReadKey();
int转枚举:
int n1 = 3;
QQState state = (QQState)n1;
Console.WriteLine(state);
Console.ReadKey();
枚举转string:
QQState state = QQState.OnLine;
string s = state.ToString();
Console.WriteLine(s);
Console.ReadKey();
string转枚举:
string s = "ABCDEFG";
//将s转换成枚举类型
//Convert.ToInt32() int.parse() int.TryParse()
//调用Parse()方法的目的就是为了让它帮助我们将一个字符串转换成对应的枚举类型
QQState state = (QQState)Enum.Parse(typeof(QQState), s);
Console.WriteLine(state);
Console.ReadKey();
枚举练习:
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();
标签:
原文地址:http://www.cnblogs.com/liuslayer/p/4450903.html