C# 权举 enum 与数据类型之间的转换

来源:互联网 发布:跨界打劫的数据 编辑:程序博客网 时间:2024/06/10 06:14
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public enum TrickScore        {             Sit = 7,            Beg = 25,            RollOver = 50,            Fetch = 10,            ComeHere = 5,            Speak = 30,        }        private void button1_Click(object sender, EventArgs e)        {            int value = (int)TrickScore.Fetch * 3;            MessageBox.Show(value.ToString());            TrickScore score = (TrickScore)value;            MessageBox.Show(score.ToString());        }    }}


第一个弹窗显示“30”,因为此时由 TrickScore 转换为了 int,value 的值是30。

第二个弹窗显示“Speak”,因为此时 value 由 int 转换为了 TrickScore,而通过 enum 的权举,代表 30 的值正好是 Speak。

0 0