Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SortedDictionary<TKey, TValue>::IDictionary::Add Method (Object^, Object^)

 

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

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

private:
virtual void Add(
	Object^ key,
	Object^ value
) sealed = 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.

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 Itemproperty 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<TKey, TValue> class through the System.Collections::IDictionary interface. The code example creates an empty SortedDictionary<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.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft