SortedList<TKey, TValue>::IDictionary::Add Method (Object^, Object^)
Adds an element with the provided key and value to the IDictionary.
Assembly: System (in System.dll)
Parameters
- key
-
Type:
System::Object^
The Object to use as the key of the element to add.
- value
-
Type:
System::Object^
The Object to use as the value of the element to add.
Implements
IDictionary::Add(Object^, Object^)| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
| ArgumentException | key is of a type that is not assignable to the key type TKey of the IDictionary. -or- value is of a type that is not assignable to the value type TValue of the IDictionary. -or- An element with the same key already exists in the IDictionary. |
You can also use the Item property to add new elements by setting the value of a key that does not exist in the dictionary; for example, myCollection["myNonexistentKey"] = myValue. However, if the specified key already exists in the dictionary, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
This method is an O(n) operation for unsorted data, where n is Count. It is an O(log n) operation if the new element is added at the end of the list. If insertion causes a resize, the operation is O(n).
The following code example shows how to access the SortedList<TKey, TValue> class through the System.Collections::IDictionary interface. The code example creates an empty SortedList<TKey, TValue> of strings with string keys and uses the IDictionary::Add method to add some elements. The example demonstrates that the IDictionary::Add method throws an ArgumentException when attempting to add a duplicate key, or when a key or value of the wrong data type is supplied.
The code example demonstrates the use of several other members of the System.Collections::IDictionary interface.
Available since 10
.NET Framework
Available since 2.0