Redigera

Dela via


OrderedDictionary.Insert(Int32, Object, Object) Method

Definition

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

public:
 virtual void Insert(int index, System::Object ^ key, System::Object ^ value);
public void Insert (int index, object key, object value);
public void Insert (int index, object key, object? value);
abstract member Insert : int * obj * obj -> unit
override this.Insert : int * obj * obj -> unit
Public Sub Insert (index As Integer, key As Object, value As Object)

Parameters

index
Int32

The zero-based index at which the element should be inserted.

key
Object

The key of the entry to add.

value
Object

The value of the entry to add. The value can be null.

Implements

Exceptions

index is out of range.

This collection is read-only.

Examples

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");
    }
}
// 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");
    }
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then

    ' 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")) Then
        myOrderedDictionary.Remove("keyToDelete")
    End If
End If

Remarks

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.

Applies to