int? number = true ? 5 : null;
无法确定条件表达式的类型,因为'int'和之间没有隐式转换
为什么不编译?诠释?数字= true? 5:空;无法确定条件表达式的类型,因为'int'和
b ? x : y
,存在三种可能性,x
和y
都具有类型和
int? num = true ? 5 : default(int?);
int? num = true ? 5 : new int?();
int? num = true ? 5 : null as int?;
int? num = true ? 5 : (int?)null;
int? num = true ? (int?)5 : null;
int? num = true ? 5 as int? : null;
int? num = true ? new int?(5) : null;
此外,在您看到int?
的任何地方,您也可以使用Nullable<int>
。