This documentation is archived and is not being maintained.

OrderedDictionary::Insert Method

Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.

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

public:
virtual void Insert(
	int index, 
	Object^ key, 
	Object^ value
) sealed

Parameters

index
Type: System::Int32
The zero-based index at which the element should be inserted.
key
Type: System::Object
The key of the entry to add.
value
Type: System::Object
The value of the entry to add. The value can be nullptr.

Implements

IOrderedDictionary::Insert(Int32, Object, Object)

ExceptionCondition
ArgumentOutOfRangeException

index is out of range.

NotSupportedException

This collection is read-only.

If the index parameter is equal to the number of entries in the OrderedDictionary collection, the key and value parameters are appended to the end of the collection.

Entries that follow the insertion point move down to accommodate the new entry and the indexes of the moved entries are also updated.

The following code example demonstrates the modification of an OrderedDictionary collection. In this example, the Insert method is used to add a new entry to the beginning of the OrderedDictionary, moving the rest of the entries down. This code is part of a larger code example that can be viewed at OrderedDictionary.


// Modifying the OrderedDictionary
if (!myOrderedDictionary->IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary->Insert(0, "insertedKey1", "insertedValue1");

    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";

    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary->RemoveAt(myOrderedDictionary->Count - 1);

    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary->Contains("keyToDelete"))
    {
        myOrderedDictionary->Remove("keyToDelete");
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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