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

要删除的元素的键。

例外

keynull

对象 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保持不变。 不会引发异常。

适用于