C# | Adding the specified key and value into HybridDictionary Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report HybridDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the HybridDictionary. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the entry to add. The value can be null. Exceptions: ArgumentNullException : If the key is null. ArgumentException : If an entry with the same key already exists in the HybridDictionary. Below given are some examples to understand the implementation in a better way : Example 1: CSHARP // C# code to add an entry with // the specified key and value // into 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); // Displaying the key/value pairs in myDict Console.WriteLine("The key/value pairs in myDict are : "); foreach(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " --> " + de.Value); } } } Output: Total key/value pairs in myDict are : 6 The key/value pairs in myDict are : Australia --> Canberra Belgium --> Brussels Netherlands --> Amsterdam China --> Beijing Russia --> Moscow India --> New Delhi Example 2: CSHARP // C# code to add an entry with // the specified key and value // into 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("I", "first"); myDict.Add("II", "second"); myDict.Add("III", "third"); myDict.Add("IV", "fourth"); myDict.Add("V", "fifth"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs in myDict are : " + myDict.Count); // Displaying the key/value pairs in myDict Console.WriteLine("The key/value pairs in myDict are : "); foreach(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " --> " + de.Value); } } } Output: Total key/value pairs in myDict are : 5 The key/value pairs in myDict are : I --> first II --> second III --> third IV --> fourth V --> fifth Note: An object that has no correlation between its state and its hash code value should typically not be used as the key. For example, String objects are better than StringBuilder objects for use as keys. A key cannot be null, but a value can. This method is an O(1) operation. When the number of elements becomes greater than the optimal size for a ListDictionary, the elements are copied from the ListDictionary to a Hashtable. However, this only happens once. If the collection is already stored in a Hashtable and the number of elements falls below the optimal size for a ListDictionary, the collection remains in the Hashtable. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.hybriddictionary.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check the HybridDictionary for a specific key S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Specialized-Namespace CSharp-Specialized-HybridDictionary Similar Reads C# | Gets or sets the value in HybridDictionary with specified key HybridDictionary.Item[Object] property is used to get or set the value associated with the specified key. Syntax: public object this[object key] { get; set; } Here, key is the key whose value is to be get or set. Return Value: The value associated with the specified key. If the specified key is not 2 min read C# | Add the specified key and value into the ListDictionary ListDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the ListDictionary. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the entry to add. The value can be null. Exceptions: Arg 2 min read C# | Count the number of key/value pairs in HybridDictionary HybridDictionary.Count property is used to get the number of key/value pairs contained in the HybridDictionary. Syntax: public int Count { get; } Return Value: The number of key/value pairs contained in the HybridDictionary. Note: Retrieving the value of this property is an O(1) operation. Below pro 2 min read C# | Check the HybridDictionary for a specific key HybridDictionary.Contains(Object) method is used to determine whether the HybridDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the HybridDictionary. Return Value: This method will return True if the HybridDictionary contains an 2 min read C# | Get an ICollection containing the values in HybridDictionary HybridDictionary.Values property is used to get an ICollection containing the values in the HybridDictionary. Syntax: public System.Collections.ICollection Values { get; } Return Value: It returns an ICollection containing the values in the HybridDictionary. Below programs illustrate the use of Hybr 2 min read C# | Removing the specified key entry from HybridDictionary HybridDictionary.Remove(Object) method is used to remove the entry with the specified key from the HybridDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry to remove. Exception: This method throws ArgumentNullException if the key is null. Below given are some exam 3 min read Like