.net 何时使用 Collection<T> 与 List<T>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/419914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
when to use Collection<T> vs List<T>
提问by AndreasKnudsen
Possible Duplicate:
What is the difference between List (of T) and Collection(of T)?
What is the best practice for when to use one vs the other?
何时使用一种与另一种的最佳实践是什么?
Both implement IList<T> and hold the data in an ordered fashion, but only List expose the sorting semantics....
两者都实现 IList<T> 并以有序的方式保存数据,但只有 List 公开排序语义....
回答by OJ.
Collection<T>:Provides the base class for a generic collection.
List<T>:Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
Collection<T>:提供泛型集合的基类。
List<T>:表示可以通过索引访问的对象的强类型列表。提供搜索、排序和操作列表的方法。
So, according the docs, one is intended as a base class for collections. The other is intended for use as a container.
因此,根据文档,一个旨在作为集合的基类。另一个用作容器。
So useList<T>and inheritfrom Collection<T>.
所以使用List<T>并继承自Collection<T>.

