IList的使用C#协变和逆变,这可能吗? [英] IList using covariance and contravariance in c#, is this possible?
本文介绍了IList的使用C#协变和逆变,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这会是可能的吗? (我没有对2010年,所以我不能尝试一下自己,对不起)
would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry)
public interface IComplexList<out TOutput, in TInput> where TOutput : TInput
{
public IEnumerator<TOutput> GetEnumerator();
public void Add(TInput item);
}
public interface IList<T> : IComplexList<T, T>
{
}
如果我得到它的权利,你可以使用它来真正实现在同一界面协变和逆变。
If I get it right, you could use this to actually implement covariance and contravariance in the same interface.
推荐答案
没有,你不能。在您的例子的IList< T>
是不变的。 的IList< T>
将需要申报在
/ 退出
是协变/逆变。这是不可能做到这一点只是继承了一些接口,这是协变的。
No, you can't. In your example IList<T>
is invariant. IList<T>
would require to declare in
/out
to be covariant/contravariant. It's not possible to do that just by inheriting some interface that is covariant.
这篇关于IList的使用C#协变和逆变,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文