【发布时间】:2012-05-16 23:29:25
【问题描述】:
假设我有一个这样的枚举:
public enum Cars
{
Audi = 0,
BMW,
Opel,
Renault,
Fiat,
Citroen,
AlfaRomeo,
}
我有机会在欧宝和雪铁龙之间迭代吗?我想将这些值作为方法的参数。
【问题讨论】:
-
var cars = Enum.GetValues(typeof (Cars)).Cast<int>().Where(i => i>=(int)Cars.Opel && i<=(int)Cars.Citroen).Cast<Cars>(); -
避免对这种枚举使用枚举。例如,如果有人插入一个值,它就会引入维护噩梦。你的软件最终会支持其他汽车似乎是相当合理的。
标签: c# enumeration