【发布时间】:2016-01-08 09:35:43
【问题描述】:
此代码无法编译。我期望它是错误的吗?
interface I{};
class A : I{};
class B : I{};
I mything = someBoolCondition ? new A : new B;
问题是编译器说它无法确定条件运算符的返回类型。
【问题讨论】:
-
试试
I mything = someBoolCondition ? new A() as I : new B() as I;。
标签: c# covariance conditional-operator