C# 二维布尔数组,检查数组是否包含false

C# 二维布尔数组,检查数组是否包含false,c#,arrays,C#,Arrays,我在C#中使用了一个二维布尔数组。我已经在上查找了Array.Exists函数,但我无法让它工作,要么因为我使用的是布尔函数,要么因为它是2D数组 bool[,] array = new bool[4, 4]; if (array.Contains(false)) {}; // This line is pseudo coded to show what I'm looking for 不知道这是否正确,但铸造每个元素似乎都有效: bool[,] a = new bool[4,4]

我在C#中使用了一个二维布尔数组。我已经在上查找了Array.Exists函数,但我无法让它工作,要么因为我使用的是布尔函数,要么因为它是2D数组

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

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

不知道这是否正确,但铸造每个元素似乎都有效:

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");
    }
bool[,]a=新bool[4,4]
{
{真的,真的,真的,真的},
{真,真,假,真},
{真的,真的,真的,真的},
{真的,真的,真的,真的},
};
如果(a.Cast()包含(false))
{
控制台。写入(“包含假”);
}
试着这样做:
if(array.ToList().Contains(false))

一般来说,如果效率不是必需的,那么转换为列表是非常有用的

尝试以下方法:

if (!a.Cast<bool>().All(b => b))
if(!a.Cast().All(b=>b))
var control=array.OfType()包含(false);
在处理多维数组时,您可以始终使用该方法。它非常有用

使用LINQ(
使用System.LINQ;
的类型
转换
数组扩展允许您测试每个数组元素是否为真,如果不是真,则可以用于触发事件

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");
}
a[0,0]=false//将此更改为“测试”
如果(!a.OfType().All(x=>x))
{
Console.Write(“包含假值”);
//做事
}
其他的
{
Console.Write(“包含所有真值”);
}

bool[*,*]
不包含
Any
的定义,并且找不到接受类型为
bool[*,*]的第一个参数的扩展方法
Any
bool[*,*]
不包含
All
的定义,并且找不到接受类型为
bool[*,*]
的第一个参数的扩展方法
All
。请确保在file@JoelCoehoorn这只适用于一维数组。现在应该更好了。
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");
}