Saturday, September 23, 2006

.NET Collection

ArrayList

@ ArrayList, like most collections, supports several ways to iterate over its contents.
Dim OneArrayList As ArrayList = New ArrayList()
OneArrayList.Add("First")
OneArrayList.Add("Second")
OneArrayList.Add("Three")
OneArrayList.Add("Four")
'Way #1

For Each OneArrayListItem As Object In OneArrayList
Console.WriteLine(OneArrayListItem.ToString)
Next
'Way #2

Dim OneEnumerator As IEnumerator = OneArrayList.GetEnumerator()
While OneEnumerator.MoveNext()
Console.WriteLine(OneEnumerator.Current)
End While
'Way #3

For Indexer As Integer = 0 To OneArrayList.Count - 1
Console.WriteLine(OneArrayList(Indexer))
Next

Dictionary

^ 有两种Standard Dictionary:HashtableSortedList,三种Specialized Dictionary:ListDictionaryHybridDictionaryOrderedDictionary

@ In general, the Hashtable class is a very efficient collection. The only problem with Hashtable is for small collections (fewer than 10 elements) it impede performance. This is where ListDictionary comes in.
@ When you know our collection is small, use a ListDictionary; when your collection is large, use a Hashtable. But what if you just do not know how large your collection is? Use HybridDictionary!
^ 干吗非得整得这么复杂?TNND,让事情简洁明了不行?我就不信性能会差十万八千里。硬件的强大处理能力不知有多少被浪费掉了!何不只用一种Dictionary来满足所有类似需求?HybridDictionary解决了所谓的HashtableListDictionary的efficiency问题。有什么意义?对于efficiency这一点我不太考虑,只当是存在Hashtable一种Dictionary就行了。

@ To accomodate you when you need a fast Dictionary but also need to keep the items in an ordered fashion, the .NET Framework supports the OrderedDictionary.
^ OrderedDictionary除了具备Hashtable的功能之外,还具备了control the order of the elements in the collection的功能。OrderedDirctionry is as if a mix of an ArrayList and a Hashtable. 所以,为了简单起见,作为一种通用解决方案,对于需要Dictionary功能的地方,我就只使用OrderedDictionary,只有有明确需求的时候在研究使用其他的Dictionary.
^ 可是,MD!我发现HashtableContainsKey()ContainsValue()两个method,可是OrderDictionary没有。虽然OrderDictionry.Contains(Key)可以实现Hashtable.ContainsKey()的功能,却实现不了 Hashtable.ContainsValue()

^ 也许应该考虑尽量使用Generic Collection吧。还没有研究透。