c# - C# create class with conditional operator
问题描述
I know maybe this not a good way to create class but I just wondered why if else working but conditional
operator not and need implicit conversion
static void Main(string[] args)
{
dynamic myclass;
if (true) /*some condition*/
{
myclass = new ClassA();
}
else
{
myclass = new ClassB();
}
myclass = true /*some condtion*/ ? new ClassA() : new ClassB(); //this line gives an error
//Type of conditional expression cannot be determined because there is no implicit conversion between 'ConsoleApp4.ClassA' and 'ConsoleApp4.ClassB'
}
解决方案
从文档:
结果和替代的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。
推荐阅读
- python - 按特定顺序组合两个文件?
- scala - 如何使用Scala计算Spark中数据框中列的开始索引和结束索引之间的行的平均值?
- powerbi - How to replace no data with 0 using measure in Power Bi
- asp.net-core - 核心 MVC MapRoute 模板和 RequestDelegate 不工作
- haskell - 在应用中,`<*>`如何用`fmap_i, i=0,1,2,...`来表示呢?
- java - Docker 容器中的 Apache Karaf 4 调试
- amazon-web-services - 在 terraform 上匹配 aws ami 和可用区
- twilio - Twilio 排队的呼叫未通过代理电话
- python - 如何从列表中动态生成类?
- python-3.x - 将函数应用于熊猫数据框 Python 3.7.1 时,多处理挂起