Pregunta

I just need to use the covariant out generic type modifier again. I had a class with a generic type and wanted to add an out but VS told me that this is only possible on interfaces.

But why can I use the out modifier only on an interface?

I helped myself in creating an interface copy of my class but this seems a little bit strange to me to only have an interface so I can use this modifier.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top