Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

C# | HybridDictionary Class

  • Last Updated : 24 Jan, 2019

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

ConstructorDescription
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.

Example:




// 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);
    }
}
Output:
III third
V fifth
II second
i first
I first
IV fourth

Properties

PropertyDescription
CountGets the number of key/value pairs contained in the HybridDictionary.
IsFixedSizeGets a value indicating whether the HybridDictionary has a fixed size.
IsReadOnlyGets a value indicating whether the HybridDictionary is read-only.
IsSynchronizedGets a value indicating whether the HybridDictionary is synchronized (thread safe).
Item[Object]Gets or sets the value associated with the specified key.
KeysGets an ICollection containing the keys in the HybridDictionary.
SyncRootGets an object that can be used to synchronize access to the HybridDictionary.
ValuesGets an ICollection containing the values in the HybridDictionary.

Example 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);
    }
}
Output:
Total key/value pairs in myDict are : 6

Example 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);
    }
}
Output:
False

Methods

MethodDescription
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:




// 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);
    }
}
Output:
A --> Apple
B --> Banana
C --> Cat
D --> Dog
E --> Elephant
F --> Fish

Example 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);
    }
}
Output:
Number of key/value pairs in myDict are : 6
Number of key/value pairs in myDict are : 5

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :