问题描述
有谁知道一个很好的资源,以简明地解释了在C#中,当他们的使用是否合适?
Does anyone know a good resource to concisely explain the different types of lists available in C# and when their usage is appropriate?
例如,列表,哈希表,字典等。
For example, List, Hashtable, Dictionaries etc.
我从来没有十分肯定的时候,我应该使用什么。
I'm never quite sure when I should be using what.
推荐答案
这些是不是所有的名单,但他们都集合。这里有一个简单的总结。
These aren't all lists, although they're all collections. Here's a quick summary.
非泛型集合(API是在对象
的方面。值类型的装箱。
Non-generic collections (API is in terms of object
. Values types are boxed.
这些大多是在System.Collections命名空间:
These are mostly in the System.Collections namespace:
- 的ArrayList :项目列表,由数组支持。快速的随机读/写访问。快速添加到尾部,如果的缓冲并不需要调整。
- 哈希表:在地图的关键价值。键是唯一的,值不必须如此。用来实现近澳GetHash code方法(1)读/写访问(除了讨厌的情况下,所有项目都使用相同的散列,或后备存储需要重建)。遍历键/值对给出了一个未predictable订单。 (好吧,有效地取消predictable。)
- 排序列表:就像一个Hashtable,但条目总是返回排序逐键顺序。存储为键/值对的列表。
- 堆栈:后进先出的集合李>
- 队列:先入先出集合李>
- 阵列:固定大小的O(1)随机访问;非通用的,但强类型的形式,以及
- ArrayList: A list of items, backed by an array. Fast random read/write access. Fast add to the tail end, if the buffer doesn't need resizing.
- Hashtable: Map from key to value. Keys are unique, values don't have to be. Uses the GetHashCode method to achieve near O(1) read/write access (aside from nasty cases where all items have the same hash, or the backing store needs rebuilding). Iterating over the key/value pairs gives an unpredictable order. (Well, effectively unpredictable.)
- SortedList: Like a Hashtable, but the entries are always returned in sorted-by-key order. Stored as a list of key/value pairs.
- Stack: Last-in-first-out collection
- Queue: First-in-first-out collection
- Array: Fixed-size O(1) random-access; non-generic, but has strongly typed forms as well
泛型集合。 (强类型的API,不会框的值类型(假设适合T)。
这些大多是在System.Collections.Generic命名空间:
- 名单,其中,T> :如ArrayList
- 字典< TKEY的,TValue>中:像哈希表
- 排序列表< TKEY的,TValue>中:像排序列表
- SortedDictionary< TKEY的,TValue>中:像排序列表,但关键的一棵树存储/值对赋予在许多情况下更好的性能。请参阅文档了解更多详情。
- 的LinkedList< T> :双向链表(快速访问的头部和尾部)
- 堆栈< T> :像堆栈
- 队列< T> :像队列
- ReadOnlyCollection还< T> :喜欢清单< T>,但给人一种只读视图
- List<T>: Like ArrayList
- Dictionary<TKey, TValue>: like Hashtable
- SortedList<TKey, TValue>: like SortedList
- SortedDictionary<TKey, TValue>: like SortedList, but stored as a tree of key/value pairs which gives better performance in many situations. See docs for more detail.
- LinkedList<T>: Doubly linked list (fast access to head and tail)
- Stack<T>: Like Stack
- Queue<T>: Like Queue
- ReadOnlyCollection<T>: Like List<T> but giving a read-only view
可能是最重要的收藏的接口的是的IEnumerable 一>(和的IEnumerable&LT; T> )。这种再presents项很像一个流重新$ P $序列psents字节序列。没有随机存取,只向前读。 LINQ到对象是在此基础上,和pretty的多所有的集合类型实现了。
Possibly the most important collection interface is IEnumerable (and IEnumerable<T>). This represents a sequence of items much like a Stream represents a sequence of bytes. There is no random access, just forward-reading. LINQ to Objects is based on this, and pretty much all collection types implement it.
这篇关于我在哪里可以了解各类.NET名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!