首页 > 解决方案 > CS0029:无法将类型“int”隐式转换为“bool”

问题描述

这是执行时 C# 中的一段代码,它给了我错误

错误:“无法将类型“int”隐式转换为“bool””

我无法理解我已将数组声明为boolean变量,并且我的代码中没有其他int变量,我的函数参数是否正确并不重要?

private static bool[,] array = new bool[41, 8];

public void SetArrayElement(int row, int col)
{
    array[row, col] = 1;
}

标签: c#type-conversionintboolean

解决方案


您声明了数组,bool因此您不能分配integer给它。您可以使用trueorfalse代替。

private static bool[,] array = new bool[41, 8]; 

public void SetArrayElement(int row, int col)
{
   array[row, col] = true; // assign either true or false.
}

推荐阅读