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将追加到集合的末尾。

插入点后面的条目会向下移动以容纳新条目,并且还会更新移动项的索引。

适用于