This documentation is archived and is not being maintained.

OrderedDictionary.Add Method

Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

public void Add (
	Object key,
	Object value
)
public final void Add (
	Object key, 
	Object value
)
public final function Add (
	key : Object, 
	value : Object
)
Not applicable.

Parameters

key

The key of the entry to add.

value

The value of the entry to add. This value can be a null reference (Nothing in Visual Basic).

Exception typeCondition

NotSupportedException

The OrderedDictionary collection is read-only.

A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

You can also use the Item property to add new elements by setting the value of a key that does not exist in the OrderedDictionary collection; however, if the specified key already exists in the OrderedDictionary, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.

The following code example demonstrates the creation and population of an OrderedDictionary collection. This code is part of a larger code example that can be viewed at OrderedDictionary.

// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("keyToDelete", "valueToDelete");
myOrderedDictionary.Add("testKey3", "testValue3");

ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, 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
Show: