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

Définition

Insère une nouvelle entrée dans la collection OrderedDictionary avec la clé et la valeur spécifiées à l'index spécifié.

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)

Paramètres

index
Int32

Index de base zéro auquel l'élément doit être inséré.

key
Object

Clé de l'entrée à ajouter.

value
Object

Valeur de l'entrée à ajouter. La valeur peut être null.

Implémente

Exceptions

index est hors limites.

Cette collection est en lecture seule.

Exemples

L’exemple de code suivant illustre la modification d’une OrderedDictionary collection. Dans cet exemple, la Insert méthode est utilisée pour ajouter une nouvelle entrée au début de , OrderedDictionaryen déplaçant le reste des entrées vers le bas. Ce code fait partie d’un exemple de code plus large qui peut être consulté à l’adresse 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

Remarques

Si le index paramètre est égal au nombre d’entrées dans la OrderedDictionary collection, les key paramètres et value sont ajoutés à la fin de la collection.

Les entrées qui suivent le point d’insertion descendent pour prendre en charge la nouvelle entrée et les index des entrées déplacées sont également mis à jour.

S’applique à