C# 中的 ListDictionary 类
csharpserver side programmingprogramming更新于 2025/8/9 14:07:17
ListDictionary 类使用单链表实现 IDictionary。建议用于通常包含少于 10 个项目的集合。
以下是 ListDictionary 类的属性 −
| Sr.No | 属性 &说明 |
|---|---|
| 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() 返回一个用于遍历 ListDictionary 的 IDictionaryEnumerator。 |
| 7 | GetHashCode() 用作默认哈希函数。(继承自 Object) |
| 8 | GetType() 获取当前实例的类型。 (继承自 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

