2D boolean array, check if the array contains false

I am using a 2D array of gates in C #. I was looking for the Array.Exists function in dot net pearls , but I cannot get it to work because I use booleans or because it is a 2D array.

bool[,] array = new bool[4, 4];

if (array.Contains(false)) {}; // This line is pseudo coded to show what I'm looking for
+3
source share
5 answers

I don’t know if this is correct or not, but each element works:

bool[,] a = new bool[4,4]
    {
        {true, true, true, true},
        {true, true, false, true},
        {true, true, true, true},
        {true, true, true, true},
    };

    if(a.Cast<bool>().Contains(false))
    {
        Console.Write("Contains false");
    }
+5
source

Try the following:

if (!a.Cast<bool>().All(b => b))
+4
source
var control = array.OfType<bool>().Contains(false);

OfType multidimensional . .

+3

LINQ (using System.Linq;) OfType Cast , , .

a[0,0] = false; //Change this to test
if (!a.OfType<bool>().All(x => x))
{
     Console.Write("Contains A False Value");
     //Do Stuff
}
else
{
     Console.Write("Contains All True Values");
}
+2

: if (array.ToList().Contains(false))

, ,

+1

All Articles