Add Method
Collapse the table of content
Expand the table of content

IDictionary.Add Method

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

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

void Add (
	TKey key,
	TValue value
)
void Add (
	TKey key, 
	TValue value
)
function Add (
	key : TKey, 
	value : TValue
)
Not applicable.

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.

Exception typeCondition

ArgumentNullException

key is a null reference (Nothing in Visual Basic).

ArgumentException

An element with the same key already exists in the IDictionary.

NotSupportedException

The IDictionary 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 class uses Comparer.Default, whereas the Dictionary class allows the user to specify the IComparer implementation to use for comparing keys.

Implementations can vary in whether they allow key to be a null reference (Nothing in Visual Basic).

The following code example creates an empty Dictionary of strings, with integer keys, and accesses it through the IDictionary 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.

// 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.");
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2016 Microsoft