Let us consider the following classes:
public interface A {}
public class B:A {}
public class C:A {}
I wanted to create an instance of either B or C in one line with a ternary operator:
A a = condition? new B() : new C();
This line doesn't compile (no implicit conversion), but casting either B
or C
with (A)
solves the problem.
While this is an easy workaround, I want to understand why that is. Is this a quirk of the language or is there a clear reason for this statement not to be valid?