使用奇怪的错误?操作者

时间:2012-01-09 11:52:05

标签: c# .net c#-4.0

  

可能重复:
  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语句的上述代码等同于什么呢?

3 个答案:

答案 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?)值以明确指定其类型。