OrderedDictionary.Insert(Int32, Object, Object) Metoda

Definicja

Wstawia nowy wpis do OrderedDictionary kolekcji z określonym kluczem i wartością w określonym indeksie.

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)

Parametry

index
Int32

Indeks oparty na zera, w którym element powinien zostać wstawiony.

key
Object

Klucz wpisu do dodania.

value
Object

Wartość wpisu do dodania. Wartość może być null.

Implementuje

Wyjątki

index jest poza zakresem.

Ta kolekcja jest tylko do odczytu.

Przykłady

Poniższy przykład kodu pokazuje modyfikację OrderedDictionary kolekcji. W tym przykładzie Insert metoda służy do dodawania nowego wpisu na początku OrderedDictionaryelementu , przenosząc resztę wpisów w dół. Ten kod jest częścią większego przykładu kodu, który można wyświetlić pod adresem 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

Uwagi

index Jeśli parametr jest równy liczbie wpisów w OrderedDictionary kolekcji, key parametry i value są dołączane na końcu kolekcji.

Wpisy, które są zgodne z punktem wstawiania, zostaną przeniesione w dół, aby pomieścić nowy wpis, a indeksy przeniesionych wpisów są również aktualizowane.

Dotyczy