Possible Duplicate:
Nullable types and the ternary operator. Why won’t this work?
This is my code which works
public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : (decimal?) null;
}
and this one doesn’t work
public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : null;
}
giving the compiler error:
Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between ‘decimal’ and ‘
<null>‘
I wonder why? any ideas?
The error is pretty clear. Both the “?” en “:” parts of that conditional operator need to have the same type or must at least be implicitly converted to the same Type. And a lone
nulldoesn’t have a good Type.Maybe you could use .FirstOrDefault(). Depending on the type of your
resthat would give anullor0m.