This documentation is archived and is not being maintained.

Hashtable.Remove Method

Removes the element with the specified key from the Hashtable.

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

'Declaration
Public Overridable Sub Remove ( _
	key As Object _
)
'Usage
Dim instance As Hashtable 
Dim key As Object

instance.Remove(key)

Parameters

key
Type: System.Object

The key of the element to remove.

Implements

IDictionary.Remove(Object)

ExceptionCondition
ArgumentNullException

key is Nothing.

NotSupportedException

The Hashtable is read-only.

-or-

The Hashtable has a fixed size.

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

This method is an O(1) operation.

The following example shows how to remove elements from the Hashtable.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesHashtable    

    Public Shared Sub Main()

        ' Creates and initializes a new Hashtable. 
        Dim myHT As New Hashtable()
        myHT.Add("1a", "The")
        myHT.Add("1b", "quick")
        myHT.Add("1c", "brown")
        myHT.Add("2a", "fox")
        myHT.Add("2b", "jumped")
        myHT.Add("2c", "over")
        myHT.Add("3a", "the")
        myHT.Add("3b", "lazy")
        myHT.Add("3c", "dog")

        ' Displays the Hashtable.
        Console.WriteLine("The Hashtable initially contains the following:")
        PrintKeysAndValues(myHT)

        ' Removes the element with the key "3b".
        myHT.Remove("3b")

        ' Displays the current state of the Hashtable.
        Console.WriteLine("After removing ""lazy"":")
        PrintKeysAndValues(myHT)
    End Sub     


    Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
        Dim de As DictionaryEntry
        For Each de In  myHT
            Console.WriteLine("    {0}:    {1}", de.Key, de.Value)
        Next de
        Console.WriteLine()
    End Sub 
End Class 


' This code produces the following output. 
'  
'The Hashtable initially contains the following: 
'    2c:    over 
'    3a:    the 
'    2b:    jumped 
'    3b:    lazy 
'    1b:    quick 
'    3c:    dog 
'    2a:    fox 
'    1c:    brown 
'    1a:    The 

'After removing "lazy": 
'    2c:    over 
'    3a:    the 
'    2b:    jumped 
'    1b:    quick 
'    3c:    dog 
'    2a:    fox 
'    1c:    brown 
'    1a:    The

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: