Nullable decimal question

Hi all,
why doesn't this piece of simple code compile:

decimal target;
decimal value;
target = decimal.TryParse("9", out value) value : null;

Error    1   
Type of conditional expression cannot be determined because there is no implicit conversion between 'decimal' and '<null>'  

Whereas this works fine:

if (decimal.TryParse("9", out value))
    target = value;
else
    target = null;




Answer this question

Nullable decimal question

  • DarkOasis

    You should explicitly cast value to decimal :

    decimal target;
    decimal value;
    target = decimal.TryParse("9", out value) (decimal )value : null;


  • Nullable decimal question