为什么当我的函数返回一个可为空的整数“int?”时,条件运算符“?:”在这里不起作用?“return null” 有效,但对于“?:”,我必须先将“null”转换为“(int?)”。
public int? IsLongName(string name) {
int length = name.Length;
// this works without problems
if (name.Length > 10) {
return null;
} else {
return name.Length;
}
// this reports:
// Type of conditional expression cannot be determined because
// there is no implicit conversion between '<null>' and 'int'
return name.Length > 10 ? null : name.Length;
}