Export (0) Print
Expand All

IDictionary<TKey, TValue>.Add Method (TKey, TValue)

Adds an element with the provided key and value to the IDictionary<TKey, TValue>.

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

void Add(
	TKey key,
	TValue value
)

Parameters

key
Type: TKey

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

value
Type: TValue

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

ExceptionCondition
ArgumentNullException

key is null.

ArgumentException

An element with the same key already exists in the IDictionary<TKey, TValue>.

NotSupportedException

The IDictionary<TKey, TValue> is read-only.

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 in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). 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.

Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey, TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

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

The following code example creates an empty Dictionary<TKey, TValue> of strings, with integer keys, and accesses it through the IDictionary<TKey, TValue> interface. The code example uses the Add method to add some elements. The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key.

This code is part of a larger example that can be compiled and executed. See System.Collections.Generic.IDictionary<TKey, TValue>.

// Create a new dictionary of strings, with string keys,  
// and access it through the IDictionary generic interface.
IDictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no  
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is  
// already in the dictionary. 
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft