[英]Someone can tell me what's the difference between List, Collection and Enumerable?
While I do a program, sometimes I've got this doubt. 当我做一个程序时,有时候我有这个疑问。 I have been using
List<T>
but I haven't used the others. 我一直在使用
List<T>
但我还没有使用其他的。
I'd like to know when each one is better to use, and under what circumstances. 我想知道每个人何时更好地使用,以及在什么情况下。
I'm sure you can read the documentation by yourself. 我相信你可以自己阅读文档。 I'll give a short summary here:
我在这里给出一个简短的总结:
IEnumerable
is an interface that exposes an enumerator over a collection. IEnumerable
是一个在集合上公开枚举器的接口。 Implement this interface if want to be able to support iteration, for example inside a foreach
loop. foreach
循环内部。 Collection
is an implementation of IEnumerable
(therefore you can iterate over it) that is generally further extended by user-defined classes that want to have collection-like behavior (ie use Add
, Remove
, Contains
, etc.). Collection
是IEnumerable
一个实现(因此你可以迭代它),它通常由想要具有类似集合的行为的用户定义类进一步扩展(即使用Add
, Remove
, Contains
等)。 It can also be used "as-is". List
is also an implementation of IEnumerable
(therefore you can iterate over it) that is generally used "as-is" as a container for objects of a certain type. List
也是IEnumerable
一个实现(因此你可以迭代它),它通常用作“原样”作为某种类型对象的容器。 Uses a dynamically adjusting array in the background and is the generic equivalent of ArrayList
. ArrayList
的通用等价物。 Note that while IEnumerable
can be both generic and non-generic, Collection
and List
can only be used as generic classes. 请注意,虽然
IEnumerable
可以是通用的也可以是非泛型的,但Collection
和List
只能用作泛型类。
MSDN可以:
List is instantiable type that holds items in a linear fashion. List是可实例化的类型,以线性方式保存项目。 (Edit: I'm told it's implemented with a dynamic array, rather than with a Linked List, which was just my guess).
(编辑:我被告知它是用动态数组实现的,而不是链接列表,这只是我的猜测)。
ICollection
/ CollectionBase
and IEnumerable
aren't directly instantiable types. ICollection
/ CollectionBase
和IEnumerable
不是直接可实例化的类型。
CollectionBase
/ ICollection
is a base class/interface that is inherited/implemented by any class that considers itself a collection and holds multiple items. CollectionBase
/ ICollection
是一个基类/接口,由任何认为自己是集合并保存多个项的类继承/实现。 CollectionBase
/ ICollection
also allows you to pass around collections without knowing their implementation. CollectionBase
/ ICollection
还允许您在不知道其实现的情况下传递集合。
IEnumerable
is also a interface that provides methods for iterating over the collection. IEnumerable
也是一个提供迭代集合的方法的接口。 IEnumerable
basically lets you use foreach
loops on the collection. IEnumerable
基本上允许你在集合上使用foreach
循环。 You can use the interface methods directly to get some C++ style iterators going, but the foreach
loop is less error prone. 您可以直接使用接口方法来获取一些C ++样式的迭代器,但是
foreach
循环不容易出错。
If you look at the definitions of the three (see below) you will notice that List
implements Enumerable
and ICollection
and ICollection
implements IEnumerable
. 如果查看三者的定义(见下文),您会注意到
List
实现了Enumerable
, ICollection
和ICollection
实现了IEnumerable
。 More clearly: 更清楚:
List
is CLASS which can store a variable number of items of the same type plus the functionality specified by the other two interfaces. List
是CLASS ,它可以存储可变数量的相同类型的项目以及其他两个接口指定的功能。
ICollection
is an interface which specifies a generic way to manipulate collections. ICollection
是一个接口,它指定了操作集合的通用方法。 NOTE: this is an interface and thus is NOT capable of being instantiated. 注意:这是一个接口,因此无法实例化。
IEnumerable
is an interface which specifies a means of iterating over a collection. IEnumerable
是一个接口,它指定迭代集合的方法。 NOTE: this is an interface and thus is NOT capable of being instantiated. 注意:这是一个接口,因此无法实例化。
Represents a strongly typed list of objects that can be accessed by index. 表示可以通过索引访问的强类型对象列表。 Provides methods to search, sort, and manipulate lists.
提供搜索,排序和操作列表的方法。
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
Defines methods to manipulate generic collections. 定义操作泛型集合的方法。
public interface ICollection<T> : IEnumerable<T>,
IEnumerable
Exposes the enumerator, which supports a simple iteration over a collection of a specified type. 公开枚举器,它支持对指定类型的集合进行简单迭代。
public interface IEnumerable<out T> : IEnumerable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.