可能重复:
条件运算符无法隐式转换?
我遇到了一种奇怪的情况,想知道我为什么要这样做.我正在使用.NET 3.5.
这有效:
short foo;
if (isValid)
foo = -1;
else
foo = getFoo();
Run Code Online (Sandbox Code Playgroud)
这不起作用:
short foo;
foo = isValid ? -1 : getFoo();
Run Code Online (Sandbox Code Playgroud)
我必须打字-1:
short foo;
foo = isValid ? (short)-1 : getFoo();
Run Code Online (Sandbox Code Playgroud)
三元表达式有何不同?它认为-1是一个需要被转换为short的int.但为什么?