ListDictionary类使用单链接列表实现IDictionary。建议用于通常少于10个项目的集合。
以下是ListDictionary类的属性-
序号 | 属性和说明 |
---|---|
1 | Count 获取ListDictionary中包含的键/值对的数量。 |
2 | IsFixedSize 获取一个值,该值指示ListDictionary是否具有固定大小。 |
3 | IsReadOnly 获取一个值,该值指示ListDictionary是否为只读。 |
4 | IsSynchronized 获取一个值,该值指示ListDictionary是否已同步(线程安全)。 |
5 | Item [Object] 获取或设置与指定对象关联的值。 |
6 | Keys 获取一个ICollection,其中包含ListDictionary中的键。 |
7 | SyncRoot 获取一个对象,该对象可用于同步对ListDictionary的访问。 |
8 | 值 获取一个ICollection,其中包含ListDictionary中的值。 |
以下是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("\nListDictionary2 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("\nListDictionary2 elements..."); foreach(DictionaryEntry d in dict2) { Console.WriteLine(d.Key + " " + d.Value); } ListDictionary dict3 = new ListDictionary(); dict3 = dict2; Console.WriteLine("\nIs 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