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:
// 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); } } } |
chevron_right
filter_none
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:
// 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); } } } |
chevron_right
filter_none
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:
Recommended Posts:
- C# | Gets or sets the value in HybridDictionary with specified key
- C# | Removing the specified key entry from HybridDictionary
- C# | Count the number of key/value pairs in HybridDictionary
- C# | Creating a HybridDictionary with specified initial size & case sensitivity
- C# | Creating an empty HybridDictionary with specified case sensitivity
- C# | Creating a Case-Sensitive HybridDictionary with specified initial size
- C# | Check the HybridDictionary for a specific key
- C# | Add the specified key and value into the ListDictionary
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Get or Set the value associated with specified key in Hashtable
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Get or set the value associated with specified key in ListDictionary
- C# | Get or set the value associated with specified key in SortedList
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | HybridDictionary Class
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if HybridDictionary is read only
- C# | Check if HybridDictionary has fixed size
- C# | Removing all entries from HybridDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

