System.Collections vs. System.Collection.Generic and System.Collections.ObjectModel

ref: http://blogs.msdn.com/kcwalina/archive/2005/09/23/Collections.aspx

Published 23 September 05 08:10 PM | kcwalina star-left-on.gifstar-right-on.gifstar-left-on.gifstar-right-on.gifstar-left-on.gifstar-right-on.gifstar-left-on.gifstar-right-on.gifstar-left-on.gifstar-right-on.gif

Many people ask how the new.NET Framework 2.0 generic collections relate to the non-generic collections we shipped before. So, here you go. The most important types are in bold font.

List<T> is basically a better ArrayList. It is optimized for speed, size, and power. Use it for majority of internal implementations whenever you need to store items in a container. Do not use it in public APIs.

Dictionary<TKey,TValue> is just a strongly typed Hashtable. Some changes were made to the hashing algorithm. The details are described here.

Collection<T> is a much better CollectionBase. Use it to expose read/write collection output from public APIs. It’s in System.Collections.ObjectModel namespace. Why in a separate namespace and why such strange name? See here.

ReadOnlyCollection<T> is a much better ReadOnlyCollectionBase. It’s in System.Collections.ObjectModel namespace.

Queue<T> and Stack<T> are equivalent to Queue and Stack.

SortedList<TKey,TValue> is basically a generic version of SortedList.

SortedDictionary<TKey,TValue> does not have a corresponding non-generic collection. It’s similar to SortedList and SortedList<TKey,TValue>, but it’s implemented as a balanced tree (red-black tree). Insertions are on average much faster, but some lookups are slightly slower and memory consumption is larger.

IEnumerable<T> is just like IEnumerable, but strongly typed. One difference is that IEnumerable<T> extends IDisposable. The reasons are described here.

ICollection<T> seems like ICollection, but it’s actually a very different abstraction. We found that ICollection was not very useful. At the same time, we did not have an abstraction that represented an read/write non-indexed collection. ICollection<T> is such abstraction and you could say that ICollection does not have an exact corresponding peer in the generic world; IEnumerable<T> is the closest.

IList<T> is just strongly typed IList. We removed the notion of IsFixedSize and the reasons are described here.

IDictionary<TKey,TValue> is roughly equivalent to IDictionary.

DictionaryBase does not have a corresponding generic type. Simply implement IDictionary<TKey,TValue> if you need a custom dictionary.

Also, we added KeyedCollection<TKey,TItem>. It’s a new collection which allows items to be indexed by both a key and an index. Use it to expose collections of items that have natural “names” (keys) from public APIs. You need to inherit from the collection to use it.

And finally many people asked for linked-list. LinkedList<T> was added to .NET Framework 2.0.

So here is the summary:

Non-Generic                        Similar Generic Type
ArrayList              List<T>
Hashtable              Dictionary<TKey,TValue>
SortedList             SortedList<TKey,TValue>
Queue                  Queue<T>
Stack                  Stack<T>
IEnumerable            IEnumerable<T>
ICollection            N/A (use IEnumerable<T> anything that extends it)
N/A                    ICollection<T>
IList                  IList<T>
CollectionBase         Collection<T>
ReadOnlyCollectionBase ReadOnlyCollection<T>
DictionaryBase         N/A (just implement IDictionary<TKey,TValue>
N/A                    SortedDictionary<TKey,TValue>
N/A                    KeyedCollection<TKey,TItem>
N/A                    LinkedList<T>

Let me know if you would like me to blog more information about any of these types.

转载于:https://www.cnblogs.com/javafun/archive/2008/03/14/1105423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值