IDictionary.Add Method

SortedDictionary(Of TKey, TValue).IDictionary.Add Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Adds an element with the provided key and value to the IDictionary.

Namespace:  System.Collections.Generic
Assembly:  System (in System.dll)

'Declaration
Private Sub Add ( _
	key As Object, _
	value As Object _
) Implements IDictionary.Add

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)

ExceptionCondition
ArgumentNullException

key is Nothing.

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(log n) operation, where n is Count.

The following code example shows how to access the SortedDictionary(Of TKey, TValue) class through the System.Collections.IDictionary interface. The code example creates an empty SortedDictionary(Of 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.

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft