//枚举转字符串
Console.WriteLine(Enum.GetName(typeof(Colors), 1));
Console.WriteLine(Enum.GetName(typeof(Colors), Colors.Blue));
Console.WriteLine(Enum.GetNames(typeof(Colors)).Length);
//字符串转枚举
object obj = (Colors)Enum.Parse(typeof(Colors),"Blue",false);
//枚举转Int
int red = (int)Colors.Red;
int green = (int)Colors.Green;
//Int转枚举
Colors col = (Colors)3;
Colors col2 = (Colors)Enum.ToObject(typeof(Colors), 2);
//判断是否存在枚举成员
bool isDef = Enum.IsDefined(typeof(Colors),"Blue");
//直接输出字符串枚举
Console.WriteLine(Colors.Yellow.ToString());
Console.ReadKey();
本文详细介绍了在编程中如何将枚举类型转换为字符串、整数,以及如何从字符串和整数转换回枚举类型。此外,还展示了如何获取枚举的所有成员名称,判断枚举成员是否存在,以及直接输出枚举类型的字符串表示。
1553

被折叠的 条评论
为什么被折叠?



