C# | Adding the specified key and value into HybridDictionary
HybridDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the HybridDictionary.
public void Add (object key, object value);
key : The key of the entry to add.
value : The value of the entry to add. The value can be null.
- 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 :
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
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
- 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.