为什么不能接口类型的列表,接受继承接口intances? [英] Why can't a list of an interface type accept intances of an inheriting interface?
问题描述
公共接口IPRIMARY {无效doBattle(); }
//一个ISecondary,是一个IPRIMARY
公共接口ISecondary:IPRIMARY {}
// ISecondary的实现也是一个IPRIMARY:
内部类SecondaryImpl:ISecondary
{
//必须的,因为这是一个IPRIMARY
公共无效doBattle(){}
}
我为什么不能这样做呢?
列表<&IPRIMARY GT;名单=新名单,LT; ISecondary>();
这将导致以下编译错误:
< BLOCKQUOTE>
参数类型'System.Collections.Generic.List'不是分配给参数类型'System.Collections.Generic.List
块引用>
我理解错误,我知道有解决方法。我只是没有看到任何明确的理由,为什么这直接转换是不允许的。中包含的 ISecondary
列表值,毕竟应该是(按扩展名),键入 IPRIMARY
的值。那么,为什么是列表< IPRIMARY>
和列表< ISecondary>
被解释为不相关类型
任何人都可以解释清楚的C#被设计成这样?
的稍微延长的理由例如:的试图做类似下面的东西,当我遇到问题就来了:
内部类节目
{
私有静态无效的主要(字串[] args)
{
// ISecondary的实例,并通过延伸,IPRIMARY:
变种mySecondaryInstance =新SecondaryImpl();
//这按预期工作:
AcceptImpl(mySecondaryInstance);
。通过延伸ISecondary的实例,这也是,
//的//列表,IPRIMARY的实例:
变种myListOfSecondaries =新列表与所述; ISecondary> {} mySecondaryInstance;
//但是,这不工作(编译错误结果):
AcceptList(myListOfSecondaries);
}
//注:IPRIMARY参数:
公共静态无效AcceptImpl(例如IPRIMARY){}
//注:类型IPRIMARY一览:
公共静态无效AcceptList(列表< IPRIMARY>名单){}
}
< DIV CLASS =h2_lin>解决方案
我为什么不能这样做呢?
列表与LT; IPRIMARY>名单=新名单,LT; ISecondary>();
块引用>
想象一下,你有过这样定义的方法:
公共无效PopulateList(列表< IPRIMARY> listToPopulate)
{
listToPopulate.Add(新的主( )); //主没有实现ISecondary!
}
如果你要传递一个会发生什么
清单< ISecondary>
作为参数
这是错误的
列表< ISecondary>
不可转让从列表< IPRIMARY方式>
的是让你出这样的烦恼的编译器的方式Given the following types:
public interface IPrimary{ void doBattle(); } // an ISecondary "is" an IPrimary public interface ISecondary : IPrimary { } // An implementation of ISecondary is also an IPrimary: internal class SecondaryImpl : ISecondary { // Required, since this is an IPrimary public void doBattle(){ } }
Why can I not do this?
List<IPrimary> list = new List<ISecondary>();
This results in the following compilation error:
Argument type 'System.Collections.Generic.List' is not assignable to parameter type 'System.Collections.Generic.List'
I understand the error, and I realize there are workarounds. I just do not see any clear reason why this direct conversion is disallowed. The values contained in a list of
ISecondary
, should after all, be (by extension) values of type ofIPrimary
.Why then areList<IPrimary>
andList<ISecondary>
being interpreted as unrelated types?Can anyone explain clearly the reasoning for C# being designed this way?
A slightly extended example: I came across the issue when trying to do something similar to the following:
internal class Program { private static void Main(string[] args) { // Instance of ISecondary, and by extention, IPrimary: var mySecondaryInstance = new SecondaryImpl(); // This works as expected: AcceptImpl(mySecondaryInstance); // List of instances of ISecondary, which are also, // by extention, instances of IPrimary: var myListOfSecondaries = new List<ISecondary> {mySecondaryInstance}; // This, however, does not work (results in a compilation error): AcceptList(myListOfSecondaries); } // Note: IPrimary parameter: public static void AcceptImpl(IPrimary instance){ } // Note: List of type IPrimary: public static void AcceptList(List<IPrimary> list){ } }
解决方案Why can I not do this?
List<IPrimary> list = new List<ISecondary>();
Imagine that you had a method defined like this:
public void PopulateList(List<IPrimary> listToPopulate) { listToPopulate.Add(new Primary()); // Primary does not implement ISecondary! }
What would happen if you were to pass it a
List<ISecondary>
as a parameter?The error that
List<ISecondary>
is not assignable fromList<IPrimary>
is the compiler's way of getting you out of such troubles.这篇关于为什么不能接口类型的列表,接受继承接口intances?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!