Invalid variance: The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList'. 'T' is covariant [duplicate]

早过忘川 提交于 2019-12-17 16:35:11

问题


Why the property get the error while the method can be compiled?

public interface IFoo {}
public interface IBar<out T> where T : IFoo {}

public interface IItem<out T> where T: IFoo
{
    // IEnumerable<IBar<T>> GetList(); // works
    IEnumerable<IBar<T>> ItemList { get; set; } // Error!
}

Error:

Invalid variance: The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList'. 'T' is covariant.


回答1:


You get the compiler error because you have a property getter (get) and a setter (set). The property getter has the T in it's output so out works, but the property setter will have the T in its input so it would need the in modifier.

Because you have out on T you need to remove the setter and it will compile:

public interface IItem<out T> where T : IFoo
{
    // IEnumerable<IBar<T>> GetList(); // works
    IEnumerable<IBar<T>> ItemList { get; } // also works
}

If your T is an in generic argument then the following would work:

public interface IItem<in T> where T : IFoo
{
    IEnumerable<IBar<T>> ItemList { set; } 
}

But you cannot have both (out,in) at the same time so you cannot have a co/contravariant property with a getter and a setter.




回答2:


The setter is not allowed, since if it were you would be able to do this:

public interface ISubFoo : IFoo { }

IItem<ISubFoo> item = //whatever
item.ItemList = new List<IBar<IFoo>>>();

which is not type safe.



来源:https://stackoverflow.com/questions/12484601/invalid-variance-the-type-parameter-t-must-be-contravariantly-valid-on-userq

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