简体   繁体   English

有人可以告诉我List,Collection和Enumerable之间的区别是什么?

[英]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: 我在这里给出一个简短的总结:

  1. 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循环内部。
  2. 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.). CollectionIEnumerable一个实现(因此你可以迭代它),它通常由想要具有类似集合的行为的用户定义类进一步扩展(即使用AddRemoveContains等)。 It can also be used "as-is". 它也可以“按原样”使用。
  3. 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可以是通用的也可以是非泛型的,但CollectionList只能用作泛型类。

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 / CollectionBaseIEnumerable不是直接可实例化的类型。

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实现了EnumerableICollectionICollection实现了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. ListCLASS ,它可以存储可变数量的相同类型的项目以及其他两个接口指定的功能。

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. 注意:这是一个接口,因此无法实例化。

List 名单

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

ICollection ICollection的

Defines methods to manipulate generic collections. 定义操作泛型集合的方法。

public interface ICollection<T> : IEnumerable<T>, 
IEnumerable

IEnumerable 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.

相关问题 有人可以告诉我AddForce和transform.translate之间的区别吗? - Can someone tell me the difference between AddForce and transform.translate? 有人能告诉我这段代码有什么问题吗? - Can someone tell me what's wrong with this code? 有人可以告诉我此反向传播实现的问题是什么 - Can someone tell me what is wrong with this back propagation implementation 有人能告诉我这意味着WriteLine(“{0,-12}”) - Can someone tell me what this means WriteLine(“{0,-12}”) 有人可以告诉我我的班级标题出了什么问题吗? - Can someone tell me what the problem is with my class header? 有人可以告诉我如何测试此方法 - Can someone tell me how to test this method 有人可以向我解释我的代码有什么问题吗? - Can someone explain to me what's wrong with my code please? 有人能告诉我为什么 (return DayName;) 会导致 ERROR 因为它是一个未分配的局部变量吗? - Can someone tell me why (return DayName;) causes an ERROR cause it's an unassigned local variable? 有人能告诉我这个疯狂的 c++ 语句在 C# 中是什么意思吗? - Can someone tell me what this crazy c++ statement means in C#? 有人能告诉我我的log4net配置/实现有什么问题吗? - Can someone tell me what is wrong with my log4net config/ implementation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM