遍历枚举Enum
本文将建立一个winform项目中遍历枚举Enum类型的简单示例,以下是详细步骤:
新建一个winform项目,简单布局一下,本例新建一个Listbox用来演示:
代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum WeekDays { Sunday = 0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, } private void Form1_Load(object sender, EventArgs e) { Type enumType = typeof(WeekDays); foreach (string str in Enum.GetNames(enumType)) { this.listBox1.Items.Add(string.Format("Key:{0}\tValue:{1}", Enum.Format(enumType, Enum.Parse(enumType, str), "d"), str)); } } }
F5运行效果如下: