using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class enumtest : System.Web.UI.Page { enum Color //枚举的使用例子 { Red, Green, Blue = 10, Black } protected void Page_Load(object sender, EventArgs e) { Label1.Text = ColorValue(Color.Red); //枚举默认输出为0 Label2.Text = ColorValue(Color.Green); //枚举自动赋予比文本上位于它前面的成员大 1 的值,这里为1 Label3.Text = ColorValue(Color.Blue); //枚举值为显示赋值,这里为10 Label4.Text = ColorValue(Color.Black);//枚举枚举自动赋予比文本上位于它前面的成员大 1 的值,这里为11 } static string ColorValue(Color c) { switch (c) { case Color.Red: return String.Format("Red = {0}", (int)c); case Color.Green: return String.Format("Green = {0}", (int)c); case Color.Blue: return String.Format("Blue = {0}", (int)c); case Color.Black: return String.Format("Black = {0}", (int)c); default: return "No color"; } } }