C# | HybridDictionary Class
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
| 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. |
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); } } |
chevron_right
filter_none
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:
// 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); } } |
chevron_right
filter_none
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); } } |
chevron_right
filter_none
Output:
False
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:
// 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); } } |
chevron_right
filter_none
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); } } |
chevron_right
filter_none
Output:
Number of key/value pairs in myDict are : 6 Number of key/value pairs in myDict are : 5
Reference:
Recommended Posts:
- C# | Creating an empty case-sensitive HybridDictionary Class
- C# | Adding the specified key and value into HybridDictionary
- C# | Gets or sets the value in HybridDictionary with specified key
- C# | Get an enumerator that iterates through the HybridDictionary
- C# | Removing all entries from HybridDictionary
- C# | Get an ICollection containing the keys in HybridDictionary
- C# | Check if HybridDictionary is read only
- C# | Removing the specified key entry from HybridDictionary
- C# | Check the HybridDictionary for a specific key
- C# | Get an ICollection containing the values in HybridDictionary
- How to get Synchronize access to the HybridDictionary in C#
- C# | Check if HybridDictionary has fixed size
- C# | Count the number of key/value pairs in HybridDictionary
- C# | Check if two HybridDictionary objects are equal
- C# | Creating an empty HybridDictionary with specified case sensitivity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



