Why do I need an Interface for Covariance (out Type)?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 02:24:38

It's extremely difficult to ensure that the class's definition is in fact covariant. It is much easier for the compiler to ensure that the interface is in fact covariant.

With a class, simply having a field that uses the generic type instantly forces the generic argument to be invariant, because as far as the compiler can tell, the value can be both read and modified. While it might be possible for classes to support variance, in practice the constraints that it would need to apply for developers to actually use it would be prohibitively difficult, much more so than simply creating a wrapping interface.

Let's consider a simple example:

public interface IWrapper<out T>
{
    T Value { get; }
}
public class Wrapper<T> : IWrapper<T>
{
    public Wrapper(T value)
    {
        Value = value;
    }
    public T Value { get; private set; }
}

The class shown above is not covariant with respect to T (until you cast it to the interface). It accepts an input value of type T (through the constructor). The interface is able to be covariant only because the constructor is not exposed through the interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!