.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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:56:12  来源:igfitidea点击:

when to use Collection<T> vs List<T>

.netlistcollectionsgenerics

提问by AndreasKnudsen

Possible Duplicate:
What is the difference between List (of T) and Collection(of T)?

可能的重复:
List (of T) 和 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>.