枚举
1) 声明位置:将枚举声明到命名空间的下面,类的外面,表示这个命名空间下,所有的类都可以使用这个枚举。
2) 枚举就是一个变量类型,int,double,string,decimal
3) 只是枚举声明,赋值,使用的方法跟那些普通的变量类型不一样
4) 默认情况下,每个值都会根据定义的顺序从0开始,自动赋值每个值一个整形。
5) 当然也可以为指定一个枚举的基础值。
声明方式,语法:
[public] enum 枚举类 { 值1, 值2, 值3 .。。。 }
代码示例:
{
//在此处声明枚举,他与结构的作用类类似,所有位置一样,但是同时,枚举也可以在结构中被调用
public enum Gender { 男, 女 } public enum Week { 星期一, 星期二, 星期三, 星期四, 星期五 } class Program { static void Main(string[] args) { Gender zhangsan = Gender.男; Console.Write(zhangsan); Week we = Week.星期一; Console.ReadKey(); } } }
常用用法:
namespace EnumConvertDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 变量 /// <summary> /// 操作类型管理对象 /// </summary> enumOperation operationMgr = enumOperation.EnumConvertToString; /// <summary> /// 颜色类型管理对象 /// </summary> enumColor colorMgr = enumColor.Red; #endregion #region 私有方法 private void DoAction() { switch (operationMgr) { case enumOperation.EnumConvertToString: this.txtResultString.Text = this.EnumConvertToString(colorMgr); break; case enumOperation.EnumConvertToInt: this.txtResultInt.Text = this.EnumConvertToInt(colorMgr).ToString(); break; case enumOperation.StringConvertToEnum: this.StringConvertToEnumProcess(); break; case enumOperation.IntConvertToEnum: this.IntConvertToEnumProcess(); break; } } /// <summary> /// 将枚举类型修改为字符串 /// </summary> /// <param name="color"></param> /// <returns></returns> private string EnumConvertToString(enumColor color) { //方法一 //return color.ToString(); //方法二 return Enum.GetName(color.GetType(), color); } /// <summary> /// 将枚举类型修改为数字 /// </summary> /// <param name="color"></param> /// <returns></returns> private int EnumConvertToInt(enumColor color) { return (int)color; } /// <summary> /// 将字符串类型转换为枚举类型 /// </summary> /// <param name="str"></param> /// <returns></returns> private enumColor StringConvertToEnum(string str) { enumColor color = enumColor.Red; try { color = (enumColor)Enum.Parse(typeof(enumColor), str); } catch (Exception ex) { MessageBox.Show(ex.Message); return color; } return color; } /// <summary> /// 将数字类型转换为枚举类型 /// </summary> /// <param name="i"></param> /// <returns></returns> private enumColor IntConvertToEnum(int i) { if (Enum.IsDefined(typeof(enumColor), i)) { return (enumColor)Enum.ToObject(typeof(enumColor), i); } return enumColor.Red; } private void StringConvertToEnumProcess() { string str = this.txtResultString.Text.Trim(); if (string.IsNullOrEmpty(str)) { this.txtResultString.Focus(); MessageBox.Show("String处不能为空"); return; } enumColor color = this.StringConvertToEnum(str); this.ChoseEnumValue(color); } private enumColor IntConvertToEnumOther(int i) { return (enumColor)i; } private void ChoseEnumValue(enumColor color) { switch (color) { case enumColor.Red: this.rdbRed.Select(); break; case enumColor.Yellow: this.rdbYellow.Select(); break; case enumColor.Green: this.rdbGreen.Select(); break; case enumColor.Blue: this.rdbBlue.Select(); break; case enumColor.White: this.rdbWhite.Select(); break; case enumColor.Black: this.rdbBlack.Select(); break; } } private void IntConvertToEnumProcess() { if (string.IsNullOrEmpty(this.txtResultInt.Text.Trim())) { this.txtResultInt.Focus(); MessageBox.Show("Int处不能为空"); return; } int number = Convert.ToInt32(this.txtResultInt.Text.Trim()); enumColor color = this.IntConvertToEnum(number); this.ChoseEnumValue(color); } #endregion #region 事件 private void Form1_Load(object sender, EventArgs e) { this.txtEnum.Text = "enumColor { Red,Yellow,Green,Blue,White,Black }"; } private void Convert_CheckedChanged(object sender, System.EventArgs e) { if (this.rdbConvertToString.Checked) this.operationMgr = enumOperation.EnumConvertToString; else if (this.rdbConvertToInt.Checked) this.operationMgr = enumOperation.EnumConvertToInt; else if (this.rdbStringConvertToEnum.Checked) this.operationMgr = enumOperation.StringConvertToEnum; else if (this.rdbIntConvertToEnum.Checked) this.operationMgr = enumOperation.IntConvertToEnum; this.DoAction(); } private void rdbColor_CheckedChanged(object sender, System.EventArgs e) { if (this.rdbRed.Checked) this.colorMgr = enumColor.Red; else if (this.rdbYellow.Checked) this.colorMgr = enumColor.Yellow; else if (this.rdbGreen.Checked) this.colorMgr = enumColor.Green; else if (this.rdbBlue.Checked) this.colorMgr = enumColor.Blue; else if (this.rdbWhite.Checked) this.colorMgr = enumColor.White; else if (this.rdbBlack.Checked) this.colorMgr = enumColor.Black; this.DoAction(); } private void txtResultInt_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\b') if (e.KeyChar < '0' || e.KeyChar > '9') e.Handled = true; } private void txtResultString_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\b') if (!((e.KeyChar >= 65 && e.KeyChar <= 90) || (97 <= e.KeyChar && e.KeyChar <= 122))) e.Handled = true; } private void btnConvert_Click(object sender, EventArgs e) { if (this.operationMgr == enumOperation.IntConvertToEnum) this.IntConvertToEnumProcess(); else if (this.operationMgr == enumOperation.StringConvertToEnum) this.StringConvertToEnumProcess(); } #endregion } public enum enumColor { Red = 1, Yellow, Green, Blue, White, Black } public enum enumOperation { EnumConvertToString, EnumConvertToInt, StringConvertToEnum, IntConvertToEnum } }