C# HybridDictionary Class
Last Updated :
04 Feb, 2025
In C#, the HybridDictionary Class is the part of the System.Collections.Specialized namespace. It is a collection that combines the features of both a Hashtable and a ListDictionary. 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.
- 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.
Example: This example demonstrates how to create a HybridDictrionary class, add key-value pairs, and iterate through it to print each key-value pair.
C#
// C# program to demonstrates the
// working of HybridDictionary class
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
static void Main()
{
// Create a HybridDictionary
HybridDictionary hd = new HybridDictionary();
// Add key-value pairs
hd.Add("1", "Geek1");
hd.Add("2", "Geek2");
hd.Add("3", "Geek3");
hd.Add("4", "Geek4");
// Iterate through the HybridDictionary and print
// each key-value pair
foreach(DictionaryEntry i in hd)
{
Console.WriteLine($"{i.Key}: {i.Value}");
}
}
}
Output1: Geek1
2: Geek2
3: Geek3
4: Geek4
Declaration of HybridDictionary
In C#, the Hybriddictionary is declared as:
HybridDictionary dictionary = new HybridDictionary();
Constructors
Example: This example demonstrates the use of HybridDictionary class with specified initial size and case-senstivity.
C#
// HybridDictioanry with specific initial
// size and case sensitivity
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a HybridDictionary with the
// specified initial size and case sensitivity.
HybridDictionary hd= new HybridDictionary(10, false);
// Adding key/value pairs
hd.Add("I", "first");
// This will not raise exception as the
// Collection is not case-insensitive
hd.Add("i", "first");
hd.Add("II", "second");
hd.Add("III", "third");
hd.Add("IV", "fourth");
hd.Add("V", "fifth");
// Displaying the key/value pairs
foreach(DictionaryEntry i in hd)
Console.WriteLine(i.Key + " " + i.Value);
}
}
Outputi first
III third
V fifth
IV fourth
I first
II second
Properties
The HybridDictionary provides several properties to access its state.
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: This example demonstrates how to get the count of key-value pairs in the dictionary.
C#
// C# program to demonstrates the Count property
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a HybridDictionary
HybridDictionary hd = new HybridDictionary();
// Adding key/value pairs
hd.Add(1, 100);
hd.Add(2, 200);
hd.Add(3, 300);
hd.Add(4, 400);
// To get count of key/value pairs
Console.WriteLine("Total key-value pairs are : "
+ hd.Count);
}
}
OutputTotal key-value pairs are : 4
Example 2: This example demonstrates how to check if a HybridDictionary is read-only.
C#
// C# program to demonstrates the isReadOnly property
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a HybridDictionary
HybridDictionary hd = new HybridDictionary();
// Adding key/value pairs
hd.Add("1", "Geek1");
hd.Add("2", "Geek2");
hd.Add("3", "Geek3");
hd.Add("4", "Geek4");
// To check whether the HybridDictionary
// is read-only
Console.WriteLine(hd.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: This example demonstrates how to copy the entries of a HybridDictioanry to a one-dimensional DictionaryEntry array using the CopyTo() Method.
C#
// 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 Geeks {
public static void Main()
{
// Creating a HybridDictionary
HybridDictionary hd = new HybridDictionary();
// Adding key-value pairs
hd.Add(1, 100);
hd.Add(2, 200);
hd.Add(3, 300);
hd.Add(4, 400);
// Creating a one-dimensional Array
DictionaryEntry[] arr = new DictionaryEntry[hd.Count];
// copying the HybridDictionary entries
// to a one-dimensional Array instance
// at the specified index
hd.CopyTo(arr, 0);
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(arr[i].Key + " --> "
+ arr[i].Value);
}
}
Output1 --> 100
2 --> 200
3 --> 300
4 --> 400
Example 2: This example demonstrates how to add, remove and check the count of key-value pairs in a HybridDictioary.
C#
// C# program to remove the entry
// with the specified key from
// the HybridDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a HybridDictionary
HybridDictionary hd = new HybridDictionary();
// Adding key/value pairs
hd.Add(1, 100 );
hd.Add(2, 200);
hd.Add(3, 300);
hd.Add(4, 400);
// Displaying the number of key/value
// pairs in HybridDictionary
Console.WriteLine("Number of key-value pairs are: "
+ hd.Count);
// Removing the entry with the
// specified key from the HybridDictionary
hd.Remove(3);
// Displaying the number of key/value
// pairs in HybridDictionary
Console.WriteLine("Number of key-value pairs are: "
+ hd.Count);
}
}
OutputNumber of key-value pairs are: 4
Number of key-value pairs are: 3
Similar Reads
C# Dictionary Class
In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value. In Dictionary, each entry consists of a key and its associated value.It provides constant tim
7 min read
C# ListDictionary Class
In C#, the ListDictionary class is the part of the System.Collections.Specialized namespace. It is a collection that stores key-value pairs. It is used in scenarios where the number of elements is relatively small and the order of insertion needs to be preserved. It uses a single LinkedList to store
5 min read
C# StringDictionary Class
In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys. Keys are always strings, and
5 min read
C# SortedDictionary Class
In C#, the SortedDictionary<TKey,TValue> class represents the collection of key/value pairs. This pair is in sorted form and the sorting is done on the key. This class is defined under System.Collections.Generic namespace. In the SortedDictionary class, the keys are immutable, always unique, a
5 min read
C# OrderedDictionary Class
In C#, the OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace. It implements both IDicitonary and ICollection interfaces.Allows indexed access to elements via both keys and numeric inde
6 min read
C# | Check if HybridDictionary has fixed size
HybridDictionary.IsFixedSize property is used to get a value that indicates whether the HybridDictionary has a fixed size or not. Syntax: public bool IsFixedSize { get; } Return Value: This property always returns false. Below are the programs to illustrate the use of HybridDictionary.IsFixedSize pr
2 min read
C# Dictionary
Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# | Check if HybridDictionary is read only
HybridDictionary.IsReadOnly property is used to get a value that indicates whether the HybridDictionary is read-only or not. Syntax: public bool IsReadOnly { get; } Return Value: This property always returns false. Below programs illustrate the use of HybridDictionary.IsReadOnly property: Example 1:
2 min read
C# | SortedDictionary.Keys Property
This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs
2 min read
C# | SortedDictionary.Count Property
This property is used to get the number of key/value pairs contained in the SortedDictionary<TKey, TValue>. Syntax: public int Count { get; } Return Value : It returns the number of key/value pairs contained in the SortedDictionary. Below are the programs to illustrate the use of above-discuss
2 min read