2011-04-02 54 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.

相關問題