IList和IList 之间有什么区别
.net中List
定义表明它实现了各种接口。
public class List : IList, ICollection, IEnumerable, IList, ICollection, IEnumerable
什么改变接口有没有T
带来IList
即如果我的一个类实现IList
而不是IList
,那么我可以将它用作我的自定义集合类吗?
List
实现IList
和IList
是为了使它可以在代码假定为IList
任何地方使用。 这将使更容易转换到通用IList
因为它更合适。 此外,如果您希望重用.net 1.1或更早版本的代码,即使您的类是在.net 2.0或更高版本的程序集中实现的,它也可以实现。
在IList
您只能将定义的(T)类型的对象放入,IList可以包含不同类型的对象
由于AdressInformation不是客户列表中的有效对象,因此下面的代码将无法编译
IList customers = new List (); customers.Add(new Customer()); customers.Add(new AdressInformation());
此代码将编译但在运行时转换exception
IList customers = new List(); customers.Add(new Customer()); customers.Add(new AdressInformation());