語言

IDictionary.Remove(Object) 方法

定義

從物件中移除指定鍵的 IDictionary 元素。

public:
 void Remove(System::Object ^ key);
public void Remove(object key);
abstract member Remove : obj -> unit
Public Sub Remove (key As Object)

參數

key
Object

關鍵是元素要移除。

例外狀況

key 是 null。

該 IDictionary 物件為唯讀。

-或-

它 IDictionary 有固定尺寸。

範例

以下程式碼範例示範如何實作此 Remove 方法。 此程式碼範例是本類別更大範例 IDictionary 的一部分。

public void Remove(object key)
{
    if (key == null) throw new ArgumentNullException("key");
    // Try to find the key in the DictionaryEntry array
    Int32 index;
    if (TryGetIndexOfKey(key, out index))
    {
        // If the key is found, slide all the items up.
        Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1);
        ItemsInUse--;
    }
    else
    {
        // If the key is not in the dictionary, just return.
    }
}
Public Sub Remove(ByVal key As Object) Implements IDictionary.Remove
    If key = Nothing Then
        Throw New ArgumentNullException("key")
    End If
    ' Try to find the key in the DictionaryEntry array
    Dim index As Integer
    If TryGetIndexOfKey(key, index) Then

        ' If the key is found, slide all the items up.
        Array.Copy(items, index + 1, items, index, (ItemsInUse - index) - 1)
        ItemsInUse = ItemsInUse - 1
    Else

        ' If the key is not in the dictionary, just return. 
    End If
End Sub

備註

若 IDictionary 物件不包含指定鍵的元素,則 IDictionary 保持不變。 沒例外拋出。

適用於