OrderedDictionary.Insert(Int32, Object, Object) Método

Definição

Insere uma nova entrada na OrderedDictionary coleção com a chave e o valor especificados no índice especificado.

public:
 virtual void Insert(int index, System::Object ^ key, System::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)

Parâmetros

index
Int32

O índice baseado em zero no qual o elemento deve ser inserido.

key
Object

A chave da entrada a ser adicionada.

value
Object

O valor da entrada a ser adicionada. O valor pode ser null.

Implementações

Exceções

index está fora do intervalo.

Esta coleção é somente leitura.

Exemplos

O exemplo de código a seguir demonstra a modificação de uma OrderedDictionary coleção. Neste exemplo, o Insert método é usado para adicionar uma nova entrada ao início do OrderedDictionary, movendo o restante das entradas para baixo. Esse código faz parte de um exemplo de código maior que pode ser exibido em 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 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

Comentários

Se o index parâmetro for igual ao número de entradas na OrderedDictionary coleção, os parâmetros e value os key parâmetros serão acrescentados ao final da coleção.

As entradas que seguem o ponto de inserção se movem para baixo para acomodar a nova entrada e os índices das entradas movidas também são atualizados.

Aplica-se a