2011-04-02 56 views
17

当我编译我的C#项目在MonoDevelop中,我得到以下错误:类型条件表达式不能被确定为

Type of conditional expression cannot be determined as 'byte' and 'int' convert implicitly to each other

代码段:

byte oldType = type; 
type = bindings[type]; 
//Ignores updating blocks that are the same and send block only to the player 
if (b == (byte)((painting || action == 1) ? type : 0)) 
{ 
    if (painting || oldType != type) { SendBlockchange(x, y, z, b); } return; 
} 

这是行在错误中突出显示:

if (b == (byte)((painting || action == 1) ? type : 0))

非常感谢帮助!

回答

23

条件运算符是一个表达式,因此需要返回类型,并且两个路径必须具有相同的返回类型。

(painting || action == 1) ? type : (byte)0 
+0

这是有道理的。非常感谢你! – Jakir00 2011-04-02 18:16:56

+1

如果参数预期为任何类型,例如String.Format(“value:{0}”,(value == null)?:“null”:value),那么该怎么办?where value is type '诠释?'? – mr5 2017-05-03 08:32:06

4

byteint之间不存在隐式转换,所以你需要指定一个三元操作符的结果:

? type : (byte)0 

在该运营商均返回类型的需要或者是相同的或者有一个为了工作而定义的隐式转换。

从MSDN ?: Operator

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

相关问题