如果.NET3。5+你可以使用System.Linq
,然后检查使用Any
:
// if it contains any false element it will return true
true_or_false.Any(x => !x); // !false == true
如果你不能使用LINQ的,那么你有其他的,选项:
使用Array.Exists
静态方法:(本·提到)
Array.Exists(true_or_false, x => !x);
使用List.Exists
(你将不得不数组转换到一个列表访问此方法)
true_or_falseList.Exists(x => !x);
或者您将需要遍历数组。
foreach (bool b in true_or_false)
{
if (!b) return true; // if b is false return true (it contains a 'false' element)
}
return false; // didn't find a 'false' element
相关
和优化代码:
bool[] true_or_false = new bool[10];
for (int i = 0; i < txtbox_and_message.Length; i++)
{
true_or_false[i] = !String.IsNullOrEmpty(txtbox_and_message[i]);
}
什么是循环?看起来它会检查空白文本(重复)并将* all *元素设置为true或false,如果字符串为空? – PaulG 2010-10-09 22:33:39
“bStatus”不应该依赖于“我”吗?这就是PaulG为什么说所有的元素都是一样的原因。 – 2010-10-09 22:37:00
[C#方法中的返回布尔值]的可能重复(http://stackoverflow.com/questions/3852078/returning-booleans-in-a-c-method) – 2010-10-09 22:38:15