检查数组是否包含false?

kru*_*hah 6 .net c# arrays contains .net-2.0

如何检查true_or_false 包含false值的数组?

bool[] true_or_false = new bool[10];

for (int i = 0; i < txtbox_and_message.Length; i++)
{
  bool bStatus = true;
  if (txtbox_and_message[i] == "")
  {
    bStatus = false;
  }
  true_or_false[i] = bStatus;                           
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*igt 15

如果它们都不是真的,那么至少有一个是假的.

因此:

!true_or_false.All(x => x)
Run Code Online (Sandbox Code Playgroud)

Docu:http://msdn.microsoft.com/en-us/library/bb548541.aspx

编辑:.NET 2.0版本,根据要求:

!Array.TrueForAll(true_or_false, delegate (bool x) { return x; })
Run Code Online (Sandbox Code Playgroud)

要么

Array.Exists(true_or_false, delegate (bool x) { return !x; })
Run Code Online (Sandbox Code Playgroud)

注意:我一直远离设置的无意义代码true_or_false,但它可能是你想要的是:

int emptyBox = Array.FindIndex(txtbox_and_message, string.IsNullOrEmpty);
Run Code Online (Sandbox Code Playgroud)

如果所有字符串都是非空的,则为-1,否则为失败字符串的索引.


Hom*_*mam 11

return true_or_false.Any(p => !p);
Run Code Online (Sandbox Code Playgroud)

  • @krunal:如果你有平台限制,你应该把它们放在问题中. (3认同)

Nob*_*ody 8

using System.Linq;
Run Code Online (Sandbox Code Playgroud)

然后:

true_or_false.Contains(false);
Run Code Online (Sandbox Code Playgroud)