Open In App

C# HybridDictionary Class

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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}");
        }
    }
}

Output
1: Geek1
2: Geek2
3: Geek3
4: Geek4

Declaration of HybridDictionary

In C#, the Hybriddictionary is declared as:

HybridDictionary dictionary = new HybridDictionary();

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

Output
i first
III third
V fifth
IV fourth
I first
II second

Properties

The HybridDictionary provides several properties to access its state.

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

Output
Total 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); 
	} 
}

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

Output
1 --> 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); 
	} 
} 

Output
Number of key-value pairs are: 4
Number of key-value pairs are: 3


Next Article

Similar Reads