HybridDictionary尝试优化哈希表。它实现了链表和哈希表数据结构。当集合很小时,它通过使用ListDictionary实现IDictionary ;而当集合很大时,它通过使用Hashtable来实现IDictionary。
HybridDictionary类的属性:
- 对于字典中元素数量未知的情况,建议使用此类。
- 它利用具有较小集合的ListDictionary的改进性能,并提供了切换到Hashtable的灵活性,该哈希表比ListDictionary更好地处理较大的集合。
- 如果集合的初始大小大于ListDictionary的最佳大小,则将集合存储在Hashtable中,以避免将元素从ListDictionary复制到Hashtable的开销。
- 键不能为null,但值可以为null。
建设者
Constructor | Description |
---|---|
HybridDictionary() | Creates an empty case-sensitive HybridDictionary. |
HybridDictionary(Boolean) | Creates an empty HybridDictionary with the specified case sensitivity. |
HybridDictionary(Int32) | Creates a case-sensitive HybridDictionary with the specified initial size. |
HybridDictionary(Int32, Boolean) | Creates a HybridDictionary with the specified initial size and case sensitivity. |
例子:
// C# code to create a HybridDictionary
// with the specified initial size
// and case sensitivity.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary with the
// specified initial size and case sensitivity.
HybridDictionary myDict = new HybridDictionary(10, false);
// Adding key/value pairs in myDict
myDict.Add("I", "first");
// This will not raise exception as the
// Collection is not case-insensitive
myDict.Add("i", "first");
myDict.Add("II", "second");
myDict.Add("III", "third");
myDict.Add("IV", "fourth");
myDict.Add("V", "fifth");
// Displaying the key/value pairs in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " " + de.Value);
}
}
输出:
III third
V fifth
II second
i first
I first
IV fourth
特性
Property | Description |
---|---|
Count | Gets the number of key/value pairs contained in the HybridDictionary. |
IsFixedSize | Gets a value indicating whether the HybridDictionary has a fixed size. |
IsReadOnly | Gets a value indicating whether the HybridDictionary is read-only. |
IsSynchronized | Gets a value indicating whether the HybridDictionary is synchronized (thread safe). |
Item[Object] | Gets or sets the value associated with the specified key. |
Keys | Gets an ICollection containing the keys in the HybridDictionary. |
SyncRoot | Gets an object that can be used to synchronize access to the HybridDictionary. |
Values | Gets an ICollection containing the values in the HybridDictionary. |
范例1:
// C# code to get the number of key/value
// pairs contained in the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("Australia", "Canberra");
myDict.Add("Belgium", "Brussels");
myDict.Add("Netherlands", "Amsterdam");
myDict.Add("China", "Beijing");
myDict.Add("Russia", "Moscow");
myDict.Add("India", "New Delhi");
// To get count of key/value pairs in myDict
Console.WriteLine("Total key/value pairs in myDict are : "
+ myDict.Count);
}
}
输出:
Total key/value pairs in myDict are : 6
范例2:
// C# code to check whether the
// HybridDictionary is read-only.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
myDict.Add("F", "Fish");
// To check whether the HybridDictionary
// is read-only.
Console.WriteLine(myDict.IsReadOnly);
}
}
输出:
False
方法
Method | Description |
---|---|
Add(Object, Object) | Adds an entry with the specified key and value into the HybridDictionary. |
Clear() | Removes all entries from the HybridDictionary. |
Contains(Object) | Determines whether the HybridDictionary contains a specific key. |
CopyTo(Array, Int32) | Copies the HybridDictionary entries to a one-dimensional Array instance at the specified index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an IDictionaryEnumerator that iterates through the HybridDictionary. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(Object) | Removes the entry with the specified key from the HybridDictionary. |
ToString() | Returns a string that represents the current object. |
范例1:
// C# code to copy the HybridDictionary
// entries to a one-dimensional Array
// instance at the specified index.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
myDict.Add("F", "Fish");
// Creating a one-dimensional Array named myArr
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
// copying the HybridDictionary entries
// to a one-dimensional Array instance
// at the specified index
myDict.CopyTo(myArr, 0);
for (int i = 0; i < myArr.Length; i++)
Console.WriteLine(myArr[i].Key + " --> " + myArr[i].Value);
}
}
输出:
A --> Apple
B --> Banana
C --> Cat
D --> Dog
E --> Elephant
F --> Fish
范例2:
// C# code to remove the entry
// with the specified key from
// the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
myDict.Add("F", "Fish");
// Displaying the number of key/value
// pairs in HybridDictionary myDict
Console.WriteLine("Number of key/value pairs in myDict are : "
+ myDict.Count);
// Removing the entry with the
// specified key from the HybridDictionary.
myDict.Remove("C");
// Displaying the number of key/value
// pairs in HybridDictionary myDict
Console.WriteLine("Number of key/value pairs in myDict are : "
+ myDict.Count);
}
}
输出:
Number of key/value pairs in myDict are : 6
Number of key/value pairs in myDict are : 5
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.hybriddictionary?view=netframework-4.7.2