IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) Method

定義

IDictionary 物件中移除具有指定索引鍵的項目。Removes the element with the specified key from the IDictionary object.

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

參數

key
Object Object Object Object

要移除的項目索引鍵。The key of the element to remove.

例外狀況

IDictionary 物件是唯讀的。The IDictionary object is read-only.

-或--or- IDictionary 具有固定的大小。The IDictionary has a fixed size.

範例

下列程式碼範例示範如何實作Remove方法。The following code example demonstrates how to implement the Remove method. 此程式碼範例是針對提供之較大範例的一部分IDictionary類別。This code example is part of a larger example provided for the IDictionary class.

public:
    virtual void Remove(Object^ key)
    {
        if (key == nullptr)
        {
            throw gcnew ArgumentNullException("key");
        }
        // Try to find the key in the DictionaryEntry array
        int index;
        if (TryGetIndexOfKey(key, &index))
        {
            // If the key is found, slide all the items down.
            Array::Copy(items, index + 1, items, index, itemsInUse -
                index - 1);
            itemsInUse--;
        }
        else
        {
            // If the key is not in the dictionary, just return.
            return;
        }
    }
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 會保持不變。If the IDictionary object does not contain an element with the specified key, the IDictionary remains unchanged. 不會有例外狀況擲回。No exception is thrown.

適用於