Export (0) Print
Expand All

Hashtable.Remove Method

Removes the element with the specified key from the Hashtable.

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

public:
virtual void Remove (
	Object^ key
)
public void Remove (
	Object key
)
public function Remove (
	key : Object
)
Not applicable.

Parameters

key

The key of the element to remove.

Exception typeCondition

ArgumentNullException

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

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.

using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( Hashtable^ myHT );
int main()
{
   
   // Creates and initializes a new Hashtable.
   Hashtable^ myHT = gcnew 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 );
}

void PrintKeysAndValues( Hashtable^ myHT )
{
   IEnumerator^ myEnum = myHT->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry de = *safe_cast<DictionaryEntry ^>(myEnum->Current);
      Console::WriteLine( "    {0}:    {1}", de.Key, de.Value );
   }

   Console::WriteLine();
}

/*
 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

 */

import System.*;
import System.Collections.*;

public class SamplesHashtable
{
    public static void main(String[] args)
    {
        // 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);
    } //main

    public static void PrintKeysAndValues(Hashtable myHT)
    {
        IEnumerator myEnumerator = myHT.GetEnumerator();
		while (myEnumerator.MoveNext()) {
			DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current();
            Console.WriteLine("    {0}:    {1}", de.get_Key(), de.get_Value());
        }
        Console.WriteLine();
    } //PrintKeysAndValues
} //SamplesHashtable 

/*
 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
 */

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 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft