Export (0) Print
Expand All

Hashtable.Remove Method

Removes the element with the specified key from the Hashtable.

[Visual Basic]
Public Overridable Sub Remove( _
   ByVal key As Object _
) Implements IDictionary.Remove
[C#]
public virtual void Remove(
 object key
);
[C++]
public: virtual void Remove(
 Object* key
);
[JScript]
public function Remove(
   key : Object
);

Parameters

key
The key of the element to remove.

Implements

IDictionary.Remove

Exceptions

Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).
NotSupportedException The Hashtable is read-only.

-or-

The Hashtable has a fixed size.

Remarks

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

Example

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

[Visual Basic] 
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(myList As Hashtable)
        Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
        Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
           + "-VALUE-")
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
               + "{1}", myEnumerator.Key, myEnumerator.Value)
        End While
        Console.WriteLine()
    End Sub
End Class

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

[C#] 
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = 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 );
   }


   public static void PrintKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The Hashtable initially contains the following:
    -KEY-    -VALUE-
    3a:    the
    3c:    dog
    3b:    lazy
    1c:    brown
    1b:    quick
    1a:    The
    2a:    fox
    2b:    jumped
    2c:    over

After removing "lazy":
    -KEY-    -VALUE-
    3a:    the
    3c:    dog
    1c:    brown
    1b:    quick
    1a:    The
    2a:    fox
    2b:    jumped
    2c:    over
*/

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintKeysAndValues( Hashtable* myList )  {
   IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
   Console::WriteLine( S"\t-KEY-\t-VALUE-" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t{0}:\t{1}", myEnumerator->Key, myEnumerator->Value );
   Console::WriteLine();
}


void main()  {

   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( S"1a", S"The" );
   myHT->Add( S"1b", S"quick" );
   myHT->Add( S"1c", S"brown" );
   myHT->Add( S"2a", S"fox" );
   myHT->Add( S"2b", S"jumped" );
   myHT->Add( S"2c", S"over" );
   myHT->Add( S"3a", S"the" );
   myHT->Add( S"3b", S"lazy" );
   myHT->Add( S"3c", S"dog" );

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

   // Removes the element with the key "3b".
   myHT->Remove( S"3b" );

   // Displays the current state of the Hashtable.
   Console::WriteLine( S"After removing \"lazy\":" );
   PrintKeysAndValues( myHT );
}


 /*
 This code produces the following output.

 The Hashtable initially contains the following:
     -KEY-    -VALUE-
     3a:    the
     3c:    dog
     3b:    lazy
     1c:    brown
     1b:    quick
     1a:    The
     2a:    fox
     2b:    jumped
     2c:    over

 After removing "lazy":
     -KEY-    -VALUE-
     3a:    the
     3c:    dog
     1c:    brown
     1b:    quick
     1a:    The
     2a:    fox
     2b:    jumped
     2c:    over
 */

[JScript] 
import System
import System.Collections
import Microsoft.VisualBasic

// Creates and initializes a new Hashtable.
var myHT : Hashtable = 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)

function PrintKeysAndValues(myList : Hashtable){
    var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
    Console.WriteLine("\t-KEY-\t-VALUE-")
    while(myEnumerator.MoveNext())
        Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
    Console.WriteLine()
}

// This code produces the following output.
// 
// The Hashtable initially contains the following:
//     -KEY-    -VALUE-
//     3a:      the
//     3c:      dog
//     3b:      lazy
//     1c:      brown
//     1b:      quick
//     1a:      The
//     2a:      fox
//     2b:      jumped
//     2c:      over
// 
// After removing "lazy":
//     -KEY-    -VALUE-
//     3a:      the
//     3c:      dog
//     1c:      brown
//     1b:      quick
//     1a:      The
//     2a:      fox
//     2b:      jumped
//     2c:      over 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Hashtable Class | Hashtable Members | System.Collections Namespace | Add | IDictionary.Remove

Show:
© 2014 Microsoft