代码
namespace EnumDemo
{
public enum Color
{
red = 0, orange =1, yellow =2, green=4, blue =5, cyan =6, purple = 7
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("------Enume.GetValues获得枚举项的数组(基类)-----");
Array ary = Enum.GetValues(typeof(Color)); //array是数组的基类, 无法实例化
foreach (int i in ary) //列出枚举项对应的数字
{
Console.WriteLine(i.ToString());
}
Console.WriteLine("------------------------------------------------");
foreach (Color c in ary) //列出枚举项的枚举值
{
Console.WriteLine(c.ToString());
}
Console.WriteLine("------ary.GetValue获得枚举项-----");
//Enumes.GetValues和ary.GetValue获得枚举项
for (int i = 0; i < ary.Length; i++)
{
Console.WriteLine(ary.GetValue(i).ToString()); //可以通过(Color)ary.GetValue(i)获得枚举项的值
}
Console.WriteLine("------Enume.GetName获得枚举项的字符串数组-----");
//Enume.GetNames和Enum.Parse获得枚举项
string[] strary = Enum.GetNames(typeof(Color));
foreach(string s in strary)
{
Console.WriteLine(s);
}
Console.WriteLine("------num.Parse获得枚举项-----");
for (int i = 0; i < strary.Length; i++)
{
Console.WriteLine(Enum.Parse(typeof(Color), strary[i]).ToString());
}
}
}
}