• 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
            }
        }
    }
  • 相关阅读:
    js中的 || 与 && 运算符详解
    区块链技术与应用:02BTC密码学原理
    C# Windows 截图上遇到过的坑
    WPF 实现弹出层
    设计个窗口定位器
    【域渗透】CVE2022–26923
    拿下某学校内网多媒体管控系统
    Pthon操作MongoDB
    MongoDB使用
    python封装MongoDB的使用
  • 原文地址:https://www.cnblogs.com/lanymy/p/2595315.html
Copyright © 2020-2023  润新知