
HybridDictionary类通过使用ListDictionary在集合较小时实现IDictionary,然后在集合变大时切换到Hashtable。
以下是HybridDictionary类的属性:
1Count
获取包含的键/值对的数量
混合字典。
2IsFixedSize
获取一个值,该值指示是否HybridDictionary 具有固定大小。
3IsReadOnly
获取一个值,指示 HybridDictionary 是否为只读。
4IsSynchronized
获取一个值,指示 HybridDictionary 是否为synchronized (线程安全)。
5Item[Object]
获取或设置与指定键关联的值。
6Keys
获取包含键的 ICollectionHybridDictionary。
7SyncRoot
获取可用于同步访问的对象到HybridDictionary。
8Values
获取包含在HybridDictionary中的值的ICollectionthe HybridDictionary.
以下是HybridDictionary类的一些方法:
1Add(Object, Object)
将具有指定键和值的条目添加到the HybridDictionary.
2Clear()
从HybridDictionary中移除所有条目。
3Contains(Object)
确定HybridDictionary是否包含特定的键。
4CopyTo(Array, Int32)
将HybridDictionary的条目复制到一个一维数组中在指定索引处的数组实例。
5Equals(Object)
确定指定的对象是否等于当前对象。(Inherited from Object)
6GetEnumerator()
返回一个 IDictionaryEnumerator,用于遍历HybridDictionary。
7GetHashCode() strong>
用作默认哈希函数。(继承自Object)
8GetType()
获取当前实例的Type。(Inherited from 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 SUVB MUVC AUVCount of Key/value pairs in Dictionary1 = 3HybridDictionary2 elements...1 One2 Two3 Three4 Four5 Five6 SixCount of Key/value pairs in Dictionary2 = 3Count 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 BooksB ElectronicsC Smart WearablesD Pet SuppliesE ClothingF FootwearIs the HybridDictionary1 having fixed size? = FalseIf HybridDictionary1 read-only? = FalseIs HybridDictionary1 synchronized = FalseHybridDictionary2 elements...1 One2 Two3 Three4 Four5 Five6 SixIs HybridDictionary1 equal to HybridDictionary2? = FalseIs the HybridDictionary2 having fixed size? = FalseIf HybridDictionary2 read-only? = FalseIs HybridDictionary2 synchronized = False
以上就是C# 中的混合字典类?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435250.html
微信扫一扫
支付宝扫一扫