有人可以告诉我列表、集合和枚举之间有什么区别吗?
我在做程序的时候,有时会有这样的疑问。我一直在使用 List
但我没有使用其他的。
我想知道每种方法何时使用以及在什么情况下使用效果更好。
While I do a program, sometimes I've got this doubt. I have been using List<T>
but I haven't used the others.
I'd like to know when each one is better to use, and under what circumstances.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你可以自己阅读文档。我将在这里给出一个简短的总结:
IEnumerable
是一个在集合上公开枚举器的接口。如果希望能够支持迭代,例如在foreach
循环内,请实现此接口。Collection
是IEnumerable
的实现(因此您可以迭代它),通常由想要具有类似集合行为的用户定义类进一步扩展(即使用 <代码>添加、删除
、包含
等)。它也可以“按原样”使用。List
也是IEnumerable
的实现(因此您可以迭代它),通常“按原样”用作特定类型对象的容器。在后台使用动态调整数组,它是 ArrayList 的通用等效项。请注意,虽然
IEnumerable
可以是泛型和非泛型,但Collection
和List
只能用作泛型类。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. Implement this interface if want to be able to support iteration, for example inside aforeach
loop.Collection
is an implementation ofIEnumerable
(therefore you can iterate over it) that is generally further extended by user-defined classes that want to have collection-like behavior (i.e. useAdd
,Remove
,Contains
, etc.). It can also be used "as-is".List
is also an implementation ofIEnumerable
(therefore you can iterate over it) that is generally used "as-is" as a container for objects of a certain type. Uses a dynamically adjusting array in the background and is the generic equivalent ofArrayList
.Note that while
IEnumerable
can be both generic and non-generic,Collection
andList
can only be used as generic classes.列表是可实例化的类型,以线性方式保存项目。 (编辑:我被告知它是用动态数组实现的,而不是用链接列表实现的,这只是我的猜测)。
ICollection
/CollectionBase
和IEnumerable
不是直接可实例化的类型。CollectionBase
/ICollection
是一个基类/接口,由任何将自己视为集合并保存多个项目的类继承/实现。CollectionBase
/ICollection
还允许您在不知道其实现的情况下传递集合。IEnumerable
也是一个提供迭代集合方法的接口。IEnumerable
基本上允许您在集合上使用foreach
循环。您可以直接使用接口方法来运行一些 C++ 风格的迭代器,但foreach
循环不太容易出错。List is instantiable type that holds items in a linear fashion. (Edit: I'm told it's implemented with a dynamic array, rather than with a Linked List, which was just my guess).
ICollection
/CollectionBase
andIEnumerable
aren't directly instantiable types.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
also allows you to pass around collections without knowing their implementation.IEnumerable
is also a interface that provides methods for iterating over the collection.IEnumerable
basically lets you useforeach
loops on the collection. You can use the interface methods directly to get some C++ style iterators going, but theforeach
loop is less error prone.MSDN 可以:
列表
IList
ICollection
IEnumerable
MSDN can:
List<T>
IList<T>
ICollection<T>
IEnumerable<T>
如果您查看这三个的定义(见下文),您会注意到
List
实现了Enumerable
和ICollection
以及ICollection
code> 实现IEnumerable
。更清楚地说:List
是CLASS,它可以存储可变数量的相同类型的项目以及其他两个接口指定的功能。ICollection
是一个接口,它指定操作集合的通用方法。注意:这是一个接口,因此无法实例化。IEnumerable
是一个接口,它指定迭代集合的方法。注意:这是一个接口,因此无法实例化。List
表示可通过索引访问的强类型对象列表。提供搜索、排序和操作列表的方法。
ICollection
定义操作通用集合的方法。
IEnumerable
公开枚举器,它支持对集合的简单迭代指定类型。
If you look at the definitions of the three (see below) you will notice that
List
implementsEnumerable
andICollection
andICollection
implementsIEnumerable
. 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.ICollection
is an interface which specifies a generic way to manipulate collections. 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. NOTE: this is an interface and thus is NOT capable of being instantiated.List
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
ICollection
Defines methods to manipulate generic collections.
IEnumerable
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.