C# 中的 HybridDictionary 类?
HybridDictionary 类通过使用 ListDictionary 实现 IDictionary 接口,当集合较小时使用 ListDictionary,当集合变大时切换到 Hashtable。
以下是 HybridDictionary 类的属性:
序号 | 属性及描述 |
---|---|
1 | Count 获取 HybridDictionary 中包含的键值对的数量。 |
2 | IsFixedSize 获取一个值,该值指示 HybridDictionary 是否具有固定大小。 |
3 | IsReadOnly 获取一个值,该值指示 HybridDictionary 是否为只读。 |
4 | IsSynchronized 获取一个值,该值指示 HybridDictionary 是否已同步(线程安全)。 |
5 | Item[Object] 获取或设置与指定键关联的值。 |
6 | Keys 获取包含 HybridDictionary 中键的 ICollection。 |
7 | SyncRoot 获取一个对象,该对象可用于同步对 HybridDictionary 的访问。 |
8 | Values 获取包含 HybridDictionary 中值的 ICollection。 |
以下是 HybridDictionary 类的一些方法:
序号 | 方法及描述 |
---|---|
1 | Add(Object, Object) 向 HybridDictionary 中添加具有指定键和值的条目。 |
2 | Clear() 移除 HybridDictionary 中的所有条目。 |
3 | Contains(Object) 确定 HybridDictionary 是否包含特定键。 |
4 | CopyTo(Array, Int32) 将 HybridDictionary 条目复制到指定索引处的一维数组实例。 |
5 | Equals(Object) 确定指定对象是否等于当前对象。(继承自 Object) |
6 | GetEnumerator() 返回一个 IDictionaryEnumerator,该枚举器遍历 HybridDictionary。 |
7 | GetHashCode() 用作默认哈希函数。(继承自 Object) |
8 | GetType() 获取当前实例的 Type。(继承自 Object) |
要计算 HybridDictionary 中键值对的数量,代码如下:
示例
现在让我们看一些示例:
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { HybridDictionary dict1 = new HybridDictionary(); dict1.Add("A", "SUV"); dict1.Add("B", "MUV"); dict1.Add("C", "AUV"); Console.WriteLine("HybridDictionary1 elements..."); foreach(DictionaryEntry d in dict1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count); HybridDictionary dict2 = new HybridDictionary(); 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("
HybridDictionary2 elements..."); foreach(DictionaryEntry d in dict2) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count); dict2.Clear(); Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count); } }
输出
这将产生以下输出:
HybridDictionary1 elements... A SUV B MUV C AUV Count of Key/value pairs in Dictionary1 = 3 HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Count of Key/value pairs in Dictionary2 = 3 Count of Key/value pairs in Dictionary2 (Updated) = 0
要检查 HybridDictionary 是否已同步,代码如下:
示例
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { HybridDictionary dict1 = new HybridDictionary(); 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("HybridDictionary1 elements..."); foreach(DictionaryEntry d in dict1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Is the HybridDictionary1 having fixed size? = "+dict1.IsFixedSize); Console.WriteLine("If HybridDictionary1 read-only? = "+dict1.IsReadOnly); Console.WriteLine("Is HybridDictionary1 synchronized = "+dict1.IsSynchronized); HybridDictionary dict2 = new HybridDictionary(); 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("
HybridDictionary2 elements..."); foreach(DictionaryEntry d in dict2) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = "+(dict1.Equals(dict2))); Console.WriteLine("Is the HybridDictionary2 having fixed size? = "+dict2.IsFixedSize); Console.WriteLine("If HybridDictionary2 read-only? = "+dict2.IsReadOnly); Console.WriteLine("Is HybridDictionary2 synchronized = "+dict2.IsSynchronized); } }
输出
这将产生以下输出:
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the HybridDictionary1 having fixed size? = False If HybridDictionary1 read-only? = False Is HybridDictionary1 synchronized = False HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is HybridDictionary1 equal to HybridDictionary2? = False Is the HybridDictionary2 having fixed size? = False If HybridDictionary2 read-only? = False Is HybridDictionary2 synchronized = False
广告