zoukankan      html  css  js  c++  java
  • c# 枚举操作 正运算 逆运算

    没用控制台写,用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
            }
        }
    }
  • 相关阅读:
    2879. [NOI2012]美食节【费用流】
    luogu P1012 拼数
    luogu cover
    luogu cogs . [NOIP2003] 传染病控制 WA(1/2)
    luogu P1340 兽径管理 WA
    luogu P1342 请柬
    HTML学习笔记二
    HTML学习笔记一
    arr.sort()
    编写函数实现随机产生指定范围的整数的功能
  • 原文地址:https://www.cnblogs.com/lanymy/p/2595315.html
Copyright © 2011-2022 走看看