可能重复:
The type of the conditional expression can not be determined?
我目前写下这句话:
byte? Col_8 = (Rad_8.SelectedValue == null) ? null : byte.Parse(Rad_8.SelectedValue);
但它有这个错误:
无法确定条件表达式的类型,因为
'<null>'
和'byte'
之间没有隐式转换
为什么我可以在?
之后使用null?如果没有if
语句的上述代码等同于什么呢?
答案 0 :(得分:8)
编译器无法推断条件语句的类型,因为null
没有类型,并且它不考虑预期的返回值。
使用
(Rad_8.SelectedValue == null) ? (byte?)null : byte.Parse(Rad_8.SelectedValue);
答案 1 :(得分:0)
if(Rad_8.SelectedValue == null)
Col_8 = null;
else
Col_8 = byte.Parse(Rad_8.SelectedValue);
答案 2 :(得分:0)
我认为这是因为方法byte.Parse(...)
没有返回可空类型,因此编译器说null
- 和byte
- 类型之间没有隐式转换。尝试使用null
转换(byte?)
值以明确指定其类型。