C#中的ListDictionary類
ListDictionary類使用單鏈表實現IDictionary介面。建議用於通常包含少於10個專案的集合。
以下是ListDictionary類的屬性:
| 序號 | 屬性及描述 |
|---|---|
| 1 | Count 獲取ListDictionary中包含的鍵/值對的數量。 |
| 2 | IsFixedSize 獲取一個值,該值指示ListDictionary是否具有固定大小。 |
| 3 | IsReadOnly 獲取一個值,該值指示ListDictionary是否為只讀。 |
| 4 | IsSynchronized 獲取一個值,該值指示ListDictionary是否已同步(執行緒安全)。 |
| 5 | Item[Object] 獲取或設定與指定鍵關聯的值。 |
| 6 | Keys 獲取包含ListDictionary中鍵的ICollection。 |
| 7 | SyncRoot 獲取可用於同步對ListDictionary訪問的物件。 |
| 8 | Values 獲取包含ListDictionary中值的ICollection。 |
以下是ListDictionary類的一些方法:
| 序號 | 方法及描述 |
|---|---|
| 1 | Add(Object, Object) 將具有指定鍵和值的條目新增到ListDictionary中。 |
| 2 | Clear() 移除ListDictionary中的所有條目。 |
| 3 | Contains(Object) 確定ListDictionary是否包含特定的鍵。 |
| 4 | CopyTo(Array, Int32) 將ListDictionary條目複製到指定索引處的一維Array例項。 |
| 5 | Equals(Object) 確定指定物件是否等於當前物件。(繼承自Object) |
| 6 | GetEnumerator() 返回一個IDictionaryEnumerator,它遍歷ListDictionary。 |
| 7 | GetHashCode() 用作預設雜湊函式。(繼承自Object) |
| 8 | GetType() 獲取當前例項的Type。(繼承自Object) |
示例
讓我們來看一些例子:
要檢查ListDictionary是否為只讀,程式碼如下:
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
Console.WriteLine("The ListDictionary1 has the key M? = "+dict1.Contains("M"));
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("
ListDictionary2 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
}
}輸出
這將產生以下輸出:
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the ListDictionary1 having fixed size? = False If ListDictionary1 read-only? = False Is ListDictionary1 synchronized = False The ListDictionary1 has the key M? = False ListDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Is the ListDictionary2 having fixed size? = False If ListDictionary2 read-only? = False Is ListDictionary2 synchronized = False The ListDictionary2 has the key 5? = True
要檢查兩個ListDictionary物件是否相等,程式碼如下:
示例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("
ListDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("
Is ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
}
}輸出
這將產生以下輸出:
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is ListDictionary3 equal to ListDictionary2? = True
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP