可能重复的问题:
Conditional operator assignment with Nullable<value> types?C#条件运算符:?有可空INT
为什么条件运算符“?:”这里没有工作时,我的函数返回一个可空整数“int?”? “返回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;
}
为什么不返回一个布尔? – 2012-04-21 17:26:45
难道我们没有更好的重复吗?还是在eric的博客上?我当然记得比这个问题更好的答案。 – CodesInChaos 2012-04-21 17:30:41
检查Eric Lippert的相关博客文章:[Type inference woes,part one](http://blogs.msdn.com/ericlippert/archive/2006/05/24/type-inference-woes-part-one.aspx) – CodesInChaos 2012-04-21 17:42:54