.NET数据结构:ArrayList中,列表,哈希表,字典,SortedList的,SortedDictionary - 速度,内存,以及何时使用的呢?数据结构、字典、内存、速度

2023-09-02 21:08:40 作者:花落尘归去

.NET有很多复杂的数据结构。不幸的是,其中有些很相似,我并不总是知道何时使用1,何时使用另一种。大部分的C#和Visual Basic的书籍谈论他们在一定程度上,但他们从来没有真正进入任何真正的细节。

.NET has a lot of complex data structures. Unfortunately, some of them are quite similar, and I'm not always sure when to use one and when to use another. Most of my C# and Visual Basic books talk about them to a certain extent, but they never really go into any real detail.

什么是阵列之间的差异,ArrayList中,列表,哈希表,字典,SortedList的和SortedDictionary?

What's the difference between Array, ArrayList, List, Hashtable, Dictionary, SortedList, and SortedDictionary?

哪些是枚举(IList的 - 可以做'的foreach'循环)?哪些使用键/值对(IDict)?

Which ones are enumerable (IList -- can do 'foreach' loops)? Which ones use key/value pairs (IDict)?

内存占用怎么样?插入的速度?检索速度?

What about memory footprint? Insertion speed? Retrieval speed?

还有没有其他的数据结构,值得一提的?

Are there any other data structures worth mentioning?

我还在寻找对内存使用和速度(大O符号)的更多细节。

I'm still searching for more details on memory usage and speed (Big-O notation).

推荐答案

我的头顶部:

阵列 - 重新presents一个老派的存储阵列 - 有点像一个别名正常键入[] 阵列。可以枚举。不能自动增长。我假设非常快的插入和retriv。速度。

Array - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insertion and retriv. speed.

的ArrayList - 自动增加数组。增加了更多的开销。可以枚举。,可能低于普通阵列,但仍然快pretty的。这些在.NET中使用了很多

ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET

列表 - 我的收藏 - 一可以与仿制药使用,所以你可以有一个强类型数组,如: 名单,其中,串> 。除此之外,行为很像的ArrayList

List - one of my favs - can be used with generics, so you can have a strongly typed array, e.g. List<string>. Other than that, acts very much like ArrayList.

的Hashtable - 普通的旧的哈希表。 O(1)O(n)的最坏情况。可以枚举的价值和键属性,并键/缬氨酸对。

Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs.

词典 - 同上只能通过仿制药,如词典&LT强类型,字符串,字符串&GT;

排序列表 - 一个排序泛型列表。放缓的插入,因为它必须从哪儿放东西。可以枚举。,可能在检索相同的,因为它没有诉诸,但缺失会比一个普通的老名单慢。

SortedList - a sorted generic list. Slowed on insertion since it has to figure out where to put things. Can enum., probably the same on retrieval since it doesn't have to resort, but deletion will be slower than a plain old list.

我倾向于使用列表词典所有的时间 - 一旦你开始使用它们强类型的泛型,它真的很难回到标准的非通用的。

I tend to use List and Dictionary all the time - once you start using them strongly typed with generics, its really hard to go back to the standard non-generic ones.

有很多其他的数据结构太 - 有 KeyValuePair 您可以使用做一些有趣的事情,有一个 SortedDictionary 这可能是也有用。

There are lots of other data structures too - there's KeyValuePair which you can use to do some interesting things, there's a SortedDictionary which can be useful as well.