编译器首先尝试评估右手表达式:
GetBoolValue() ? 10 : null
的
10是一个
int文字(未
int?),并
null是,那么,
null。这两者之间没有隐式转换,因此会出现错误消息。
如果将右侧表达式更改为以下表达式之一,则它将进行编译,因为
int?和
null(#1)之间
int以及和
int?(#2,#3)之间存在隐式转换。
GetBoolValue() ? (int?)10 : null // #1GetBoolValue() ? 10 : (int?)null // #2GetBoolValue() ? 10 : default(int?) // #3


![可空类型和三元运算符:为什么?10:禁止使用null?[重复] 可空类型和三元运算符:为什么?10:禁止使用null?[重复]](http://www.mshxw.com/aiimages/31/594645.png)
