VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • C# 枚举(4)

4.将字符串转换成枚举类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
amespace _04枚举和int以及string类型之间的转换
{
    public enum QQState
    {
        OnLine=1,
        OffLine,
        Leave,
        Busy,
        QMe
    }
    class Program
    {
        static void Main(string[] args)
        {
          //枚举练习
            //提示用户选择一个在线状态,我们接受,并将用户的输入转换成枚举类型。
            //再次打印到控制台中
 
            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();
}
}
}   

相关教程