How does cast in C#/.NET 3.5 work for types with '?'

Posted by Inez on Stack Overflow See other posts from Stack Overflow or by Inez
Published on 2010-06-09T15:53:31Z Indexed on 2010/06/09 16:12 UTC
Read the original article Hit count: 190

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ