A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-hybriddictionary-class/ below:

C# HybridDictionary Class - GeeksforGeeks

C# HybridDictionary Class

Last Updated : 11 Jul, 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.

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

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.

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

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

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4