• 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
            }
        }
    }
  • 相关阅读:
    沉痛的一天
    PowerBuilder之5年经验谈(一之1)--PB对Unicode的支持
    C# Client API for Sphinx (support to 0.99)
    F#学习笔记基本类型
    F#学习笔记方法
    接口串联
    eclipse 中如何设置注释?
    软件测试过程中手机截屏
    Postan中执行接口时使用JSON数据,那么什么是 JSON?
    MySQL使用dump备份以及恢复备份
  • 原文地址:https://www.cnblogs.com/lanymy/p/2595315.html
Copyright © 2020-2023  润新知