Possible Duplicate:
Nullable types and the ternary operator. Why won’t this work?如何在C#/ .NET 3.5中使用帶'?'的類型
這是我的代碼工作
public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : (decimal?) null;
}
,這一次不起作用
public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : null;
}
給編譯器錯誤:
Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'decimal' and '
<null>
'
我不知道爲什麼?有任何想法嗎?
重複http://stackoverflow.com/questions/858080的
Count
會,因爲它擊中的第一個元素遍歷而Any
所有元素,就立即停止/ nullable-types-and-the-ternary-operator-why-wont-this-work – LukeH 2010-06-09 16:09:40...和http://stackoverflow.com/questions/2450866/why-doesnt-the-conditional-operator-correctly-allow使用空分配 – LukeH 2010-06-09 16:11:07
...和http://stackoverflow.com/questions/75746/conditional-operator-assignment-with-nullablevalue-types – LukeH 2010-06-09 16:11:43