没用控制台写,用WPF写的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WpfDemoNew
{
    /// <summary>
    /// Window23.xaml 的交互逻辑
    /// </summary>
    public partial class Window23 : Window
    {
        public Window23()
        {
            InitializeComponent();

            TimeOfDay time = TimeOfDay.Afternoon;

            MessageBox.Show(Convert.ToInt32(time).ToString());//根据枚举值,取对应的索引值,输出Afternoon的索引值1
            MessageBox.Show(TimeOfDay.Afternoon.ToString());//输出Afternoon

            time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "2",true);//忽略大小写匹配,根据索引值取值

            MessageBox.Show(time.ToString()); //输出Evening

            time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "Evening", true);//忽略大小写匹配,根据值取值

            MessageBox.Show(time.ToString()); //输出Evening

            time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "12", true);//忽略大小写匹配,根据索引值取值

            MessageBox.Show(time.ToString()); //超出TimeOfDay的最大索引值,输出12

            MessageBox.Show(Enum.GetName(typeof(TimeOfDay), 1)); //根据索引值取值,输出Afternoon
            //MessageBox.Show(Enum.GetName(typeof(TimeOfDay), "Afternoon")); //根据值取值,不支持报错.
            MessageBox.Show(Enum.GetName(typeof(TimeOfDay), 10)); //超出TimeOfDay的最大索引值,输出空字符串

        }

        public enum TimeOfDay
        {
            Moning = 0,
            Afternoon = 1,
            Evening = 2
        }
    }
}
posted on 2012-07-17 15:38  ﹎蓝言觅ぷ雨  阅读(1229)  评论(1编辑  收藏  举报