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 參數附加在集合的末尾。

緊隨插入點後的條目會向下移動以容納新條目,移動條目的索引也會同步更新。

適用於