何时使用Collection< T> vs List< T>

何时使用Collection< T> vs List< T>

问题描述:


可能重复:

什么时候使用一个vs谁的最佳实践?

What is the best practice for when to use one vs the other?

两者都实现IList< T&

Both implement IList<T> and hold the data in an ordered fashion, but only List expose the sorting semantics....


  • 集合< T>

    • Collection<T>:

      提供通用
      集合的基类。

      Provides the base class for a generic collection.


    • 列表< T>

    • List<T>:

      表示可以通过索引访问的
      对象的强类型列表。
      提供搜索,排序和
      操作列表的方法。

      Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


  • 因此,根据文档,一个意图作为集合的基类。另一个用作容器。

    So, according the docs, one is intended as a base class for collections. The other is intended for use as a container.

    所以使用 列表< T> 继承来自集合< T>