. net有很多复杂的数据结构。不幸的是,其中一些非常相似,我不总是确定什么时候使用一个,什么时候使用另一个。我的大多数c#和VB书籍都在一定程度上讨论了它们,但它们从来没有真正深入任何细节。

数组、数组列表、列表、哈希表、字典、排序列表和排序字典之间的区别是什么?

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

内存占用呢?插入速度?检索速度?

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

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


当前回答

泛型集合和非泛型集合之间存在微妙和不那么微妙的差异。它们只是使用了不同的底层数据结构。例如,Hashtable保证了一个写入器-多个读取器而不需要同步。字典没有。

其他回答

泛型集合和非泛型集合之间存在微妙和不那么微妙的差异。它们只是使用了不同的底层数据结构。例如,Hashtable保证了一个写入器-多个读取器而不需要同步。字典没有。

如果可能的话,使用泛型。这包括:

List而不是ArrayList 字典而不是哈希表

我发现微软文档集合和数据结构页面上的“选择一个集合”部分非常有用

c#集合和数据结构:选择一个集合

还有下面的矩阵来比较一些其他的特征

下面是给你的一些建议:

You can use foreach on types that implement IEnumerable. IList is essentially an IEnumberable with Count and Item (accessing items using a zero-based index) properties. IDictionary on the other hand means you can access items by any-hashable index. Array, ArrayList and List all implement IList. Dictionary, SortedDictionary, and Hashtable implement IDictionary. If you are using .NET 2.0 or higher, it is recommended that you use generic counterparts of mentioned types. For time and space complexity of various operations on these types, you should consult their documentation. .NET data structures are in System.Collections namespace. There are type libraries such as PowerCollections which offer additional data structures. To get a thorough understanding of data structures, consult resources such as CLRS.

泛型集合将比非泛型集合执行得更好,特别是在遍历许多项时。这是因为装箱和开箱不再发生。