Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check whether array contains false?

How to check the array true_or_false containing a value of 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;                           
}
like image 560
krunal shah Avatar asked Oct 09 '10 22:10

krunal shah


1 Answers

If they are not all true, then at least one is false.

Therefore:

!true_or_false.All(x => x)

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

EDIT: .NET 2.0 version, as requested:

!Array.TrueForAll(true_or_false, delegate (bool x) { return x; })

or

Array.Exists(true_or_false, delegate (bool x) { return !x; })

NOTE: I've been staying away from the nonsensical code that sets true_or_false, but it could be that what you want is:

int emptyBox = Array.FindIndex(txtbox_and_message, string.IsNullOrEmpty);

which will give you -1 if all the strings are non-empty, or the index of the failing string otherwise.

like image 175
Ben Voigt Avatar answered Nov 13 '22 07:11

Ben Voigt