It simply is the definition of ?:
to require equal types.
You could of course use
? (ICollection) new List()
: (ICollection) new LinkedList();
Or just use an if/else
.
According to the C# reference, § 14.13,
[Given] A conditional expression of the form b ? x : y
- If X and Y are the same type, then this is the type of the conditional expression.
- Otherwise, if an implicit conversion (§13.1) exists from X to Y, but not from Y to X, then Y is the type of
the conditional expression.
- Otherwise, if an implicit conversion (§13.1) exists from Y to X, but not from X to Y, then X is the type of
the conditional expression.
- Otherwise, no expression type can be determined, and a compile-time error occurs.
In your case, both X and Y have a conversion to Z, but that doesn't help. It is a general principle in the language that the compiler does not even look at the target variable when applying the rules. Simple example: double a = 7 / 2; // a becomes 3.0
So after reading this I thing it would suffice to cast just 1 of the results to ICollection
. I didn't test that.