class Program { static void Main(string[] args) { Color color = Color.Red; Console.WriteLine(color.ToString()); Console.WriteLine(color.ToString("G"));//Red Console.WriteLine(color.ToString("D"));//十进制数值 Console.WriteLine(color.ToString("X"));//十六进制 Console.WriteLine(Enum.Format(typeof(Color),3,"G")); Console.WriteLine(Enum.Format(typeof(Color), Color.Red, "G")); //获取枚举的数组单个类型为Color Color[] colors = (Color[])Enum.GetValues(typeof(Color)); foreach (var item in colors) { Console.WriteLine("{0,5:D} {0:G}",item); } //获取枚举的对象的字符串 string[] names = Enum.GetNames(typeof(Color)); foreach (var item in names) { Console.WriteLine("{0,5:D} {0:G}", item); } //使用Parse将符号转化成Color类型,下面两种形式都可以 //Color c = (Color)Enum.Parse(typeof(Color),"1"); Color c = (Color)Enum.Parse(typeof(Color), "Red"); Console.WriteLine(c.ToString()); //用于参数验证,使用了反射机制 bool isEnum = Enum.IsDefined(typeof(Color), 1); Console.WriteLine(isEnum); //将指定的值转化为枚举的枚举成员 //Color r = (Color)Enum.ToObject(typeof(Color), 1); //如果枚举类型中没有的枚举成员,仍然不会报错,输出10 Color r = (Color)Enum.ToObject(typeof(Color), 10); Console.WriteLine(r.ToString()); Console.Read(); } } //默认是从0开始赋值 internal enum Color { White, Red, Green, Blue, Orange }