2

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?

OB1
  • 377
  • 1
  • 13
  • 1
    This is a duplicate of http://stackoverflow.com/questions/14144131/understanding-c-sharp-compilation-error-with-ternary-operator – Sean Jun 08 '16 at 15:16

0 Answers0