有人可以告诉我列表、集合和枚举之间有什么区别吗?

发布于 2024-12-22 03:42:42 字数 104 浏览 3 评论 0原文

我在做程序的时候,有时会有这样的疑问。我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-12-29 03:42:42

我相信你可以自己阅读文档。我将在这里给出一个简短的总结:

  1. IEnumerable 是一个在集合上公开枚举器的接口。如果希望能够支持迭代,例如在 foreach 循环内,请实现此接口。
  2. CollectionIEnumerable 的实现(因此您可以迭代它),通常由想要具有类似集合行为的用户定义类进一步扩展(即使用 <代码>添加、删除包含等)。它也可以“按原样”使用。
  3. List 也是 IEnumerable 的实现(因此您可以迭代它),通常“按原样”用作特定类型对象的容器。在后台使用动态调整数组,它是 ArrayList 的通用等效项。

请注意,虽然 IEnumerable 可以是泛型和非泛型,但 CollectionList 只能用作泛型类。

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. Implement this interface if want to be able to support iteration, for example inside a foreach loop.
  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 (i.e. use Add, Remove, Contains, etc.). 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. Uses a dynamically adjusting array in the background and is the generic equivalent of ArrayList.

Note that while IEnumerable can be both generic and non-generic, Collection and List can only be used as generic classes.

怎言笑 2024-12-29 03:42:42

列表是可实例化的类型,以线性方式保存项目。 (编辑:我被告知它是用动态数组实现的,而不是用链接列表实现的,这只是我的猜测)。

ICollection/CollectionBaseIEnumerable 不是直接可实例化的类型。

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

IEnumerableis also a interface that provides methods for iterating over the collection. IEnumerable basically lets you use foreach loops on the collection. You can use the interface methods directly to get some C++ style iterators going, but the foreach loop is less error prone.

薄暮涼年 2024-12-29 03:42:42

MSDN 可以:

荆棘i 2024-12-29 03:42:42

如果您查看这三个的定义(见下文),您会注意到 List 实现了 EnumerableICollection 以及 ICollection code> 实现 IEnumerable。更清楚地说:

ListCLASS,它可以存储可变数量的相同类型的项目以及其他两个接口指定的功能。

ICollection 是一个接口,它指定操作集合的通用方法。注意:这是一个接口,因此无法实例化。

IEnumerable 是一个接口,它指定迭代集合的方法。注意:这是一个接口,因此无法实例化。

List

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

public class List<T> : IList<T>, ICollection<T>, 
IEnumerable<T>, IList, ICollection, IEnumerable

ICollection

定义操作通用集合的方法。

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

IEnumerable

公开枚举器,它支持对集合的简单迭代指定类型。

public interface IEnumerable<out T> : IEnumerable

If you look at the definitions of the three (see below) you will notice that List implements Enumerable and ICollection and ICollection implements 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.

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.

public class List<T> : IList<T>, ICollection<T>, 
IEnumerable<T>, IList, ICollection, IEnumerable

ICollection

Defines methods to manipulate generic collections.

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

IEnumerable

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

public interface IEnumerable<out T> : IEnumerable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文