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