Mono Class Library: System.Collections.Generic.Dictionary<TKey,TValue> Overview | Members

System.Collections.Generic.Dictionary<TKey,TValue>.Add Method

Adds an element with the specified key and value to the dictionary.

public void Add (TKey key, TValue value)

Parameters

key
The key of the element to add to the dictionary.
value
The value of the element to add to the dictionary.

Exceptions

TypeReason
ArgumentExceptionAn element with the same key already exists in the dictionary.
ArgumentNullExceptionkey is null.

Remarks

You can also use the Dictionary<TKey, TValue>.Item(TKey) property to add new elements by setting the value of a key that does not exist in the dictionary. However, if the specified key already exists in the dictionary, setting the Dictionary<TKey, TValue>.Item(TKey) property overwrites the old value. In contrast, the Dictionary<TKey, TValue>.Add(TKey,TValue) method does not modify existing elements.

If Dictionary<TKey, TValue>.Count already equals the capacity, the capacity of the dictionary is increased.

A key cannot be ., but a value can be, if the value type TValue is a reference type.

Requirements

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0