HybridDictionary attempts to optimize Hashtable. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when the collection is small, and a Hashtable when the collection is large.
Properties of HybridDictionary Class:
- This class is recommended for cases where the number of elements in a dictionary is unknown.
- It takes advantage of the improved performance of a ListDictionary with small collections, and offers the flexibility of switching to a Hashtable which handles larger collections better than ListDictionary.
- If the initial size of the collection is greater than the optimal size for a ListDictionary, the collection is stored in a Hashtable to avoid the overhead of copying elements from the ListDictionary to a Hashtable.
- A key cannot be null, but a value can.
Constructors
Example:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary(10, false );
myDict.Add( "I" , "first" );
myDict.Add( "i" , "first" );
myDict.Add( "II" , "second" );
myDict.Add( "III" , "third" );
myDict.Add( "IV" , "fourth" );
myDict.Add( "V" , "fifth" );
foreach (DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " " + de.Value);
}
}
|
Output:
III third
V fifth
II second
i first
I first
IV fourth
Properties
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. |
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
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" );
Console.WriteLine( "Total key/value pairs in myDict are : "
+ myDict.Count);
}
}
|
Output:
Total key/value pairs in myDict are : 6
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
Console.WriteLine(myDict.IsReadOnly);
}
}
|
Methods
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. |
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
myDict.CopyTo(myArr, 0);
for ( int i = 0; i < myArr.Length; i++)
Console.WriteLine(myArr[i].Key + " --> " + myArr[i].Value);
}
}
|
Output:
A --> Apple
B --> Banana
C --> Cat
D --> Dog
E --> Elephant
F --> Fish
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
Console.WriteLine( "Number of key/value pairs in myDict are : "
+ myDict.Count);
myDict.Remove( "C" );
Console.WriteLine( "Number of key/value pairs in myDict are : "
+ myDict.Count);
}
}
|
Output:
Number of key/value pairs in myDict are : 6
Number of key/value pairs in myDict are : 5
Reference: