if (
(someFlagEnum1 && !foo1()) ||
(someFlagEnum2 && !foo2()) ||
(someFlagEnum3 && !foo3()) ||
(someFlagEnum4 && !bar4()) ||
(someFlagEnum5 && !bar5()) ||
(someFlagEnum6 && !bar6()) ||
(someFlagEnum7 && !bar7()) ||
(someFlagEnum8 && !bar8())
)
{
return;
}
更簡單,更難讀!混淆C#的道路從這裏開始! :-) :-)
解決方法二(C#4.0,適用於其他變種):
var conditions = new Tuple<Func<bool>, Func<bool>>[] {
new Tuple<Func<bool>, Func<bool>>(() => someFlagEnum.flag1,() => foo1()),
new Tuple<Func<bool>, Func<bool>>(() => someFlagEnum.flag2,() => foo2()),
new Tuple<Func<bool>, Func<bool>>(() => someFlagEnum.flag3,() => foo3()),
new Tuple<Func<bool>, Func<bool>>(() => someFlagEnum.flag4,() => foo4()),
new Tuple<Func<bool>, Func<bool>>(() => someFlagEnum.flag5,() => foo5())
}
foreach (var cond in conditions) {
if (cond.Item1() && !cond.Item2()) {
return;
}
}
爲什麼會使用'foreach'影響你使用'return'語句並提前退出的能力? – 2011-03-10 17:30:23
如果我錯了,請糾正我。 – lifeweaver 2011-03-10 17:35:35
'someFlagEnum'是一個真正的'System.Enum'類型嗎? – 2011-03-10 17:44:27