System.Collections.IDictionary.Add Method

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

Syntax

public void Add (object key, object value)

Parameters

key
The object to use as the key of the element to add.
value
The object to use as the value of the element to add.

Exceptions

TypeReason
ArgumentNullException key is null.
ArgumentException An entry with the same key already exists in the current instance.
NotSupportedExceptionThe current instance is read-only or has a fixed size.

Remarks

You can also use the IDictionary.Item(object) 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 IDictionary.Item(object) property overwrites the old value. In contrast, the IDictionary.Add(object, object) method does not modify existing elements.

Implementations can vary in whether they allow the key to be null.

Requirements

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0