OrderedDictionary.Insert(Int32, Object, Object) Метод

Определение

Вставляет новую запись в OrderedDictionary коллекцию с указанным ключом и значением по указанному индексу.

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)

Параметры

index
Int32

Отсчитываемый от нуля индекс, по которому должен быть вставлен элемент.

key
Object

Ключ добавляемой записи.

value
Object

Значение добавляемой записи. Значение может быть null.

Реализации

Исключения

index выходит за пределы диапазона.

Эта коллекция доступна только для чтения.

Примеры

В следующем примере кода демонстрируется изменение OrderedDictionary коллекции. В этом примере Insert метод используется для добавления новой записи в начало, OrderedDictionaryпереместив остальные записи вниз. Этот код является частью более крупного примера кода, который можно просмотреть по 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

Комментарии

index Если параметр равен количеству записей в OrderedDictionary коллекции, key к концу коллекции добавляются параметры и value параметры.

Записи, которые следуют за точкой вставки, двигаются вниз, чтобы вместить новую запись и индексы перемещаемых записей также обновляются.

Применяется к