比較表
| Collection | Dictionary | Collection<T> | Dictionary<T,U> | |
| 基本型 | ArrayList | Hashtable | List<T> | Dictionary<T,U> |
| 字串型別 | StringCollection | StringDictionary :1 key -> 1 value NameValueCollection :1 key -> n value | List<T> | Dictionary<T,U> |
| Bool型別 | BitArray | |||
| 連續型的 | Stack :LIFO Queue :FIFO | Stack<T> Queue<T> | ||
| item 順序 | SortedList :順序依 key 值自動排序 OrderedDictionary :順序維持加入的順序 | SortedList<T,U> SortedDictionaryt<T,U> Dictionary<T,U> | ||
| 存取效能 | ListDictionary :適合項目小於 10 個的集合。 HybridDictionary :項目無法預期時,這個類別會自動調整。 | Dictionary<T,U> |
集合關係圖
集合與泛型集合對照表
以下三個型別的集合,都不會保留項目的順序,而且列舉集合時不保證以特定順序列出。
| Hashtable | StringDictionary | NameValueCollection | |
|---|---|---|---|
| foreach | Hashtable myCol = new Hashtable();
myCol.Add("111", "apple");
myCol.Add("333", "book");
myCol.Add("222", "cherry");
myCol.Add("444", "dog");
//myCol.Add("444", "egg"); disallow duplicate key
foreach (DictionaryEntry item in myCol)
{
Console.WriteLine("{0} {1}", item.Key, item.Value);
}
//222 cherry
//444 dog
//111 apple
//333 book
| StringDictionary myCol = new StringDictionary();
myCol.Add("111", "apple");
myCol.Add("333", "book");
myCol.Add("222", "cherry");
myCol.Add("444", "dog");
//myCol.Add("444", "egg"); disallow duplicate key
foreach (DictionaryEntry item in myCol)
{
Console.WriteLine("{0} {1}", item.Key, item.Value);
}
//222 cherry
//444 dog
//111 apple
//333 book
| NameValueCollection myCol = new NameValueCollection();
myCol.Add("111", "apple");
myCol.Add("333", "book");
myCol.Add("222", "cherry");
myCol.Add("444", "dog");
myCol.Add("444", "egg");
foreach (string key in myCol)
{
Console.WriteLine("{0} {1}", key, myCol[key]);
}
//111 apple
//333 book
//222 cherry
//444 dog,egg
|
| IEnumerator | IEnumerator myEnumerator = myCol.GetEnumerator();
DictionaryEntry de;
while (myEnumerator.MoveNext())
{
de = (DictionaryEntry)myEnumerator.Current;
Console.WriteLine("{0} {1}", de.Key, de.Value);
}
| IEnumerator myEnumerator = myCol.GetEnumerator();
DictionaryEntry de;
while (myEnumerator.MoveNext())
{
de = (DictionaryEntry)myEnumerator.Current;
Console.WriteLine("{0} {1}", de.Key, de.Value);
}
| IEnumerator myEnumerator = myCol.GetEnumerator();
DictionaryEntry de;
while (myEnumerator.MoveNext())
{
de = (DictionaryEntry)myEnumerator.Current;
Console.WriteLine("{0} {1}", de.Key, de.Value);
}
|
| foreach key | foreach (var key in myCol.Keys)
{
Console.WriteLine("{0}", key);
}
foreach (var value in myCol.Values)
{
Console.WriteLine("{0}", value);
}
| foreach (var key in myCol.Keys)
{
Console.WriteLine("{0}", key);
}
foreach (var value in myCol.Values)
{
Console.WriteLine("{0}", value);
}
| //Print Keys And Values
foreach (string key in myCol.AllKeys)
{
Console.WriteLine("{0} {1}", key, myCol[key]);
}
//111 apple
//333 book
//222 cherry
//444 dog,egg
|
| index | 不支援 | 不支援 | for (int i = 0; i < myCol.Count; i++)
{
// myCol[i] = myCol.Get(i) = myCol[myCol.GetKey(i)]
Console.WriteLine("{0} {1}", myCol.GetKey(i), myCol[i]);
Console.WriteLine("{0} {1}", myCol.GetKey(i), myCol.Get(i));
Console.WriteLine("{0} {1}", myCol.GetKey(i), myCol[myCol.GetKey(i)]);
}
// 使用 myCol[i] , 回傳的是該項目的 value
//111 apple
//333 book
//222 cherry
//444 dog,egg
|
沒有留言:
張貼留言