检查协/对方差时会忽略约束吗?

时间:2019-06-03 13:07:31

标签: c# covariance contravariance

在查看此类代码时,我看不到其他解释:

public interface IParam
{

}
public class Param : IParam
{

}
public interface ITest<out T> where T : IParam
{

}
 public class Test : ITest<Param>
{

}

public class Tester
{
    public void Run<O>(ITest<O> a)
        where O : IParam
    {
        Get(a); // ERROR
        ITest<Param> b = new Test();
        Get(b); // OK
    }
    public void Get(ITest<IParam> input)
    {

    }
}

尽管a中的两种类型b都是通过IParam派生的类型参数有效地声明的,但这仅是第二遍编译。

到目前为止,我还没有找到通知说在处理协方差/协方差时会忽略约束。我是否缺少某些东西,或者确实是这种情况(忽略了约束)?

0 个答案:

没有答案