Enum.GetValues(enumType)、Enum.GetName(enumType, i) 2个方法
class Program
{
public enum Product
{
飞机 = 1,
轮船 = 2,
汽车 = 3
}
static void Main(string[] args)
{
List<Way> myList = new List<Way>();
foreach (Product pd in Enum.GetValues(typeof(Product)))
{
//myway对象必须放在foreach中,因为如果放在外层,只定义一个Way对象,对该对象重复赋值
//指向的是同一块内存区域,最终获得的List值是3个 “汽车 3”
Way myway = new Way();
myway.Id = pd.GetHashCode(); //1 2 3
myway.Name = Enum.GetName(typeof(Product), pd); //或者pd.ToString() 飞机,轮船,汽车
myList.Add(myway);
}
//输出List列表中的枚举值
foreach (Way wy in myList)
{
Console.WriteLine(wy.Id + " " + wy.Name);
}
Console.ReadKey();
}
}
public class Way
{
public int Id { get; set; }
public string Name { get; set; }
}