Udostępnij za pośrednictwem


IDictionary.Remove Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Removes the element with the specified key from the IDictionary object.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Sub Remove ( _
    key As Object _
)
void Remove(
    Object key
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

key is nulla null reference (Nothing in Visual Basic).

NotSupportedException

The IDictionary object is read-only.

-or-

The IDictionary has a fixed size.

Remarks

If the IDictionary object does not contain an element with the specified key, the IDictionary remains unchanged. No exception is thrown.

Examples

The following code example demonstrates how to implement the Remove method. This code example is part of a larger example provided for the IDictionary class.

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
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. 
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.