c# - 寻找有关差异问题的解释

标签 c# covariance variance

这个问题在这里已经有了答案:





Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.DBNull'

(1 个回答)


6年前关闭。




...毕竟,这实际上可能不是差异问题。当我编译我的代码时,Visual Studio 会给我以下错误:

Type of conditional expression cannot be determined because there is no implicit conversion between 'ClassA' and 'ClassB'



我在这里阅读了这个错误,听起来确实不可能将抽象类用作接口(interface)之类的契约,我可以在其中使用派生类代替它们的基类。为了使事情更简单,我编写了一些模拟实际代码关系的测试类。请考虑以下几点:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassImplicitConversion {
    class Program {
        static void Main(string[] args) {
            StartClass temp = new StartClass();
        }
    }

    public class StartClass
    {
        public AbstractClass _ac { get; private set; }

        public StartClass()
        {
            bool which = true;
            // this block will compile
            /*
            if( which)
                _ac = new DerivedClass1();
            else 
                _ac = new DerivedClass2();
             */
            // this block will not compile
            _ac = which ? new DerivedClass1() : new DerivedClass2();
        }
    }

    public interface SomeInterface
    {
        void SiFoo();
    }

    public abstract class InnerAbstractClass
    {
        public abstract void IacFoo();
    }

    public abstract class AbstractClass : InnerAbstractClass, SomeInterface
    {
        public override void IacFoo() {

        }

        public void SiFoo() {

        }

        public abstract void OverrideMe();
        public abstract void OverrideMeToo<T>();
    }

    public class DerivedClass1 : AbstractClass
    {
        public override void OverrideMe() {}
        public override void OverrideMeToo<T>() {}
    }

    public class DerivedClass2 : AbstractClass
    {
        private int _blah;
        public override void OverrideMe() {}
        public override void OverrideMeToo<T>() {}
    }
}

问题出在这条线上:
_ac = which ? new DerivedClass1() : new DerivedClass2();

如果我使用内联 if,我会得到那个可怕的编译器错误。但是,如果我改用这个:
if( which)
    _ac = new DerivedClass1();
else 
    _ac = new DerivedClass2();

我的代码编译得很好。

谁能解释这是什么原因?我一直认为两者是等价的。谢谢!

最佳答案

这正是语言标准的定义方式:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.


DerivedClass1 之间没有隐式转换和 DerivedClass2 - 它仅通过存在的基类。运算符的规范并没有说它会查看赋值的结果值类型,因此编译器正在执行它的设计目标。

来源:https://msdn.microsoft.com/en-us/library/ty67wk28.aspx

关于c# - 寻找有关差异问题的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404758/

相关文章:

python - Pandas 方差和标准差结果与手动计算不同

c# - 二进制格式化程序生成比预期更大的数组

c# - 如何制作条件安装程序以在本地或联网计算机上查找或安装 SQL Server?

c# - WPF 绑定(bind)用空格替换零

c# - "no implicit reference conversion"即使定义了转换

c# - out 关键字如何与类型协方差相关联?

c# - 派生类的通用 KeyedCollection

c# - 无法从 Derived<Derived 2T> 转换为 BaseT<base 2T>

c# - 为什么下面这个简单的 Rx.NET 不打印任何数字?

在单个解析中计算方差和标准差