可能重复:
条件运算符不能隐式转换?
使用条件运算符编写语句时,如果任一表达式是数值,则它们始终被解释为int
类型。这使得使用此运算符分配short
变量时必须进行强制转换。
bool isTrue = true;
int intVal = isTrue ? 1 : 2;
short shortVal = isTrue ? 1 : 2; // Compile error: Cannot implicitly convert type 'int' to 'short'.
难道编译器不应该short
像在典型的赋值语句(short shortVal = 1;
)中那样知道这两个值都是有效值吗?