+2  A: 

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 null doesn't have a good Type.

Maybe you could use .FirstOrDefault(). Depending on the type of your res that would give a null or 0m.

Hans Kesting
+1  A: 

I'm guessing res is an array of decimals i.e. decimal[]. This would be because you have declared res like this:

var res = { 1.0, 2.0, 3.0 };

and not like this:

var res = { 1.0, 2.0, null };

so there is no reason for the compiler to think res is an array of nullable decimals.

Now you are using a ternary operator which must always return the same (or an equivalent castable) type from both sides.

But as res.First() is a 'decimal' and your null by default is untyped it just makes your null equivalent to the type of your first argument (res.First() i.e. a decimal). By forcing the null to be typed as a nullable decimal ('decimal?') you are actually forcing the compiler to treat res.First() as a nullable decimal too.

However, a better solution overall for you is this:

public decimal? Get()
{
    decimal?[] res = ...
    return res.FirstOrDefault();
}
Graphain
The "better solution overalll" assumes `res` contains nullable decimals, which you've already assumed *is not* the case at the beginning of your answer. Given a world where `res` may not contain nullable decimals, then the "better overall solution" is incorrect.
Anthony Pegram
Great point - I thought FirstOrDefault would return a NULL but default(decimal) is in fact 0.0M.
Graphain
+7  A: 

This behavior is covered in section 7.13 of the C# language spec.

In short the types of the two expressions in the ternary operator must be * compatible* in order for the compiler to determine the type of the ternary expression. In this case the two types being considered are

  1. decimal
  2. null

The decimal type is a value type and hence null is not convertible to it. The value null has no associated type. This prevents the compiler from determining the type of the expression and leads to a compilation error.

In the first example the second type is decimal?. There is a conversion between decimal and decimal? so the compiler picks decimal? as the type.

JaredPar
A: 

In second case you can use res.FirstOrDefault().

IEnumerable<T>.FirstOrDefault() returns first T in set or default<T> if set is empty: null for classes and some default value for structs, in common case - 0.

public decimal? Get()
{
    var res = ...
    return res.FirstOrDefault();
}

which is equal to

public decimal? Get()
{
    var res = ...
    return res.Count() > 0 ? res.First() : default(decimal?);
}
abatishchev
`FirstOrDefault()` for a `decimal` list would be 0M, regardless of whether you cast the value to `decimal?` afterwards. This presents a problem. Is 0M a case of a value not existing or is 0M the first value in the list?
Anthony Pegram
+2  A: 

Off-topic, but...

Using Count is a bit pointless when all you need to know is whether or not the sequence has any elements. Count will loop through all the elements whereas Any will stop as soon as it hits the first element:

public decimal? Get()
{
    var res = ...
    return res.Any() ? res.First() : (decimal?)null;
}

Or maybe this:

public decimal? Get()
{
    var res = ...
    using (var e = res.GetEnumerator())
    {
        return e.MoveNext() ? e.Current : (decimal?)null;
    }
}
LukeH