Adding the specified key and value into HybridDictionary in C#

The HybridDictionary class in C# provides a collection that combines the functionality of a ListDictionary for small collections and a Hashtable for larger collections. It automatically switches between these internal implementations based on the number of elements to optimize performance.

HybridDictionary is part of the System.Collections.Specialized namespace and uses a ListDictionary when the collection is small (typically under 10 items) and switches to a Hashtable when it grows larger.

Syntax

Following is the syntax for adding key-value pairs to a HybridDictionary −

HybridDictionary dictionary = new HybridDictionary();
dictionary.Add(key, value);

Parameters

  • key − The key of the element to add. Cannot be null.

  • value − The value of the element to add. Can be null.

HybridDictionary Internal Structure Small Collection Uses ListDictionary < 10 items Large Collection Uses Hashtable ? 10 items Auto Switch Benefits: ? Optimized performance for both small and large collections ? Automatic internal structure management

Using Add() Method

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        HybridDictionary dict = new HybridDictionary();
        dict.Add("A", "Bags");
        dict.Add("B", "Electronics");
        dict.Add("C", "Smart Wearables");
        dict.Add("D", "Pet Supplies");
        Console.WriteLine("HybridDictionary elements...");
        foreach(DictionaryEntry d in dict){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Is the HybridDictionary having fixed size? = " + dict.IsFixedSize);
        Console.WriteLine("Is HybridDictionary read-only? = " + dict.IsReadOnly);
    }
}

The output of the above code is −

HybridDictionary elements...
A Bags
B Electronics
C Smart Wearables
D Pet Supplies
Is the HybridDictionary having fixed size? = False
Is HybridDictionary read-only? = False

Working with Multiple HybridDictionaries

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        HybridDictionary dict1 = new HybridDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");
        dict1.Add("C", "Smart Wearables");
        dict1.Add("D", "Pet Supplies");
        dict1.Add("E", "Clothing");
        dict1.Add("F", "Footwear");
        Console.WriteLine("HybridDictionary1 elements...");
        foreach(DictionaryEntry d in dict1){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of Key/value pairs in Dictionary1 = " + dict1.Count);
        
        HybridDictionary dict2 = new HybridDictionary();
        dict2.Add("1", "One");
        dict2.Add("2", "Two");
        dict2.Add("3", "Three");
        dict2.Add("4", "Four");
        dict2.Add("5", "Five");
        dict2.Add("6", "Six");
        Console.WriteLine("\nHybridDictionary2 elements...");
        foreach(DictionaryEntry d in dict2){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of Key/value pairs in Dictionary2 = " + dict2.Count);
        dict2.Clear();
        Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = " + dict2.Count);
    }
}

The output of the above code is −

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of Key/value pairs in Dictionary1 = 6

HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 6
Count of Key/value pairs in Dictionary2 (Updated) = 0

Key Features

Property Description
IsFixedSize Returns false - HybridDictionary can grow dynamically
IsReadOnly Returns false - HybridDictionary allows modifications
Count Gets the number of key-value pairs in the collection
Clear() Removes all elements from the HybridDictionary

Conclusion

HybridDictionary in C# provides an efficient collection that automatically optimizes its internal structure based on size. Use the Add() method to insert key-value pairs, and the collection will handle the performance optimization internally by switching between ListDictionary and Hashtable implementations.

Updated on: 2026-03-17T07:04:36+05:30

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements