I have following classes:
abstract class AClass { }
class Foo : AClass { }
class Bar : AClass { }
And when I am trying to use them:
AClass myInstance;
myInstance = true ? new Foo() : new Bar();
This code won't compiling because of the "Type of conditional expression cannot be determined because there is no implicit conversion between 'CSharpTest.Class1.Foo' and 'CSharpTest.Class1.Bar'"
But following samples compiling ok:
if (true)
{
myInstance = new Foo();
}
else
{
myInstance = new Bar();
}
This is ok too:
myInstance = true ? (AClass) new Foo() : new Bar();
or
myInstance = true ? new Foo() : (AClass) new Bar();
Why there is so big difference in behavior of the conditional operator and if clause?