C#中的ListDictionary类


ListDictionary类使用单链表实现IDictionary接口。建议用于通常包含少于10个项目的集合。

以下是ListDictionary类的属性:

序号属性及描述
1Count
获取ListDictionary中包含的键/值对的数量。
2IsFixedSize
获取一个值,该值指示ListDictionary是否具有固定大小。
3IsReadOnly
获取一个值,该值指示ListDictionary是否为只读。
4IsSynchronized
获取一个值,该值指示ListDictionary是否已同步(线程安全)。
5Item[Object]
获取或设置与指定键关联的值。
6Keys
获取包含ListDictionary中键的ICollection。
7SyncRoot
获取可用于同步对ListDictionary访问的对象。
8Values
获取包含ListDictionary中值的ICollection。

以下是ListDictionary类的一些方法:

序号方法及描述
1Add(Object, Object)
将具有指定键和值的条目添加到ListDictionary中。
2Clear()
移除ListDictionary中的所有条目。
3Contains(Object)
确定ListDictionary是否包含特定的键。
4CopyTo(Array, Int32)
将ListDictionary条目复制到指定索引处的一维Array实例。
5Equals(Object)
确定指定对象是否等于当前对象。(继承自Object)
6GetEnumerator()
返回一个IDictionaryEnumerator,它遍历ListDictionary。
7GetHashCode()
用作默认哈希函数。(继承自Object)
8GetType()
获取当前实例的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

更新于:2019-12-16

浏览量:302

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.