HybridDictionary.Add Method
Adds an entry with the specified key and value into the HybridDictionary.
Assembly: System (in System.dll)
abstract Add : key:Object * value:Object -> unit override Add : key:Object * value:Object -> unit
Parameters
- key
- Type: System.Object
The key of the entry to add.
- value
- Type: System.Object
The value of the entry to add. The value can be a null reference (Nothing in Visual Basic).
Implements
IDictionary.Add(Object, Object)| Exception | Condition |
|---|---|
| ArgumentNullException | key is a null reference (Nothing in Visual Basic). |
| ArgumentException | An entry with the same key already exists in the HybridDictionary. |
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 a null reference (Nothing in Visual Basic), but a value can.
You can also use the Item property to add new elements by setting the value of a key that does not exist in the HybridDictionary; for example, myCollection["myNonexistentKey"] = myValue. However, if the specified key already exists in the HybridDictionary, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
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.
This method is an O(1) operation.
The following code example adds to and removes elements from a HybridDictionary.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.