【发布时间】:2013-07-10 13:21:04
【问题描述】:
据此贴Elegantly determine if more than one boolean is "true"
使用这部分代码...
public bool ExceedsThreshold(int threshold, IEnumerable<bool> bools)
{
int trueCnt = 0;
foreach(bool b in bools)
if (b && (++trueCnt > threshold))
***//here i need to know which of b is the true***
return true;
return false;
}
我想知道哪个 bools 变量是真的?
【问题讨论】:
-
我不确定我是否理解这个问题。当您说您想知道哪些布尔变量为真时,您是指索引(例如:索引 1、3 和 4 为真)吗?
-
which one is true是什么意思。严格来说,IEnumerable不保证顺序,所以检索真假值的索引是不对的。在我看来,您可以从IEnumerable<bool>获得的唯一信息是对真值、假值和总值的计数,仅此而已。 -
是的 Chrischu 和 quetzacotel 使用 ElementAt 或其他方法向我展示了一个真实的列表或单个元素
-
@KekuSemau 一个
IEnumerable肯定是 订购的。什么会让你觉得它不是。 -
这是拒绝对象的味道;你打算用索引做什么?在另一个列表中查找其他值?
标签: c# visual-studio-2010