public enum StatusCode
{
成功 = 200,
失败 = 17
}
一:使用Foreach循环遍历
foreach (int code in Enum.GetValues(typeof(StatusCode)))
{
string name=Enum.GetName(typeof(StatusCode), code );//获取名称
string val= code .ToString();//获取值
Lsit myLi = new List(name,val);
}
二:使用for循环
for (int i = 0; i < Enum.GetValues(typeof(StatusCode)).Length; i++)
{
Items.Add(Enum.GetValues(typeof(StatusCode)).GetValue(i));
}
实例1:
public enum FillMoneyAgentType
{
Alipay = 10,
AlipayWAP = 15,
Yeepay = 20,
YingBao = 21,
YiJiFu = 22,
Tenpay = 30,
KuanQian = 40,
ChinaPay = 50,
CallsPay = 60,
ManualAdd = 80
}
List<string> aTypes = new List<string>();
Array values = Enum.GetValues(typeof(FillMoneyAgentType));
string[] ArNames = Enum.GetNames(typeof(FillMoneyAgentType));
方式一:
for (int i = 0; i < values.Length; i++)
{
var s = values.GetValue(i).ToString();
var intVal= (int)Enum.Parse(typeof(FillMoneyAgentType), s);
}
方式二:
foreach (int codeVal in values)
{
var st = codeVal.ToString();
aTypes.Add(st);
}