简体   繁体   English

CS0029:无法将类型'int'隐式转换为'bool'

[英]CS0029: Cannot implicitly convert type 'int' to 'bool'

Here is a chunk of code in C# on execution it gives me error 这是执行时C#中的一大段代码,它给了我错误

ERROR: "Cannot implicitly convert type “int” to “bool” " 错误:“无法将类型“ int”隐式转换为“布尔””

I am unable to understand that I have declared array as boolean variable and there is no other int variable in my code and it doesn't matter what my function arguments are right? 我无法理解我已将数组声明为boolean变量,并且我的代码中没有其他int变量,并且我的函数参数是正确的没关系吗?

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

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

You declared array as bool so you can not assign integer to it. 您将数组声明为bool因此无法为其分配integer You can use true or false instead. 您可以改用truefalse

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

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

Conversion from int to bool might result in lost information. int转换为bool可能会导致信息丢失。 1 is an integer literal in C#. 1是C#中的整数文字 You can use true instead. 您可以改用true

array[row, col] = true;

Unlike C , C# has special bool type and doesn't cast implicitly 1 to true : C不同, C#具有特殊的bool类型,并且不会隐式将1强制转换true

  bool myValue = 1; // <- Compile Time Error (C#)

Even if explicit cast is possible, it's not a good idea: 即使可以进行显式强制转换,也不是一个好主意:

  bool myValue = (bool)1; // It compiles, but not a good style  

In your case you can just assign true 在您的情况下,您可以分配true

  //DONE: static : we don't want "this" here
  public static void SetArrayElement(int row, int col)
  {
     //DONE: validate public method's values
     if (row < array.GetLowerBound(0) || row > array.GetUpperBound(0))
         throw new ArgumentOutOfRangeException(nameof(row));
     else if (col < array.GetLowerBound(1) || col > array.GetUpperBound(1))
         throw new ArgumentOutOfRangeException(nameof(col)); 

     array[row, col] = true; // true, instead of 1
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39;-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 我得到了 CS0029 C# 无法将类型“int”隐式转换为“bool” - i got CS0029 C# Cannot implicitly convert type 'int' to 'bool' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' 错误 CS0029:无法将类型“bool”隐式转换为“(bool valorA, bool valorB)”c# - error CS0029: Cannot implicitly convert type 'bool' to '(bool valorA, bool valorB)' c# 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM