This documentation is archived and is not being maintained.

Hashtable.Clear Method

Removes all elements from the Hashtable.

[Visual Basic]
Public Overridable Sub Clear() Implements IDictionary.Clear
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

Implements

IDictionary.Clear

Exceptions

Exception Type Condition
NotSupportedException The Hashtable is read-only.

Remarks

Count is set to zero. The capacity remains unchanged.

Example

The following example shows how to clear the values of 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("one", "The")
        myHT.Add("two", "quick")
        myHT.Add("three", "brown")
        myHT.Add("four", "fox")
        myHT.Add("five", "jumped")
        
        ' Displays the count and values of the Hashtable.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myHT.Count)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(myHT)
        
        ' Clears the Hashtable.
        myHT.Clear()
        
        ' Displays the count and values of the Hashtable.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myHT.Count)
        Console.WriteLine("   Values:")
        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.
' 
' Initially,
'    Count    : 5
'    Values:
'     -KEY-    -VALUE-
'     five:    jumped
'     three:    brown
'     four:    fox
'     two:    quick
'     one:    The
' 
' After Clear,
'    Count    : 0
'    Values:
'     -KEY-    -VALUE- 

[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( "one", "The" );
      myHT.Add( "two", "quick" );
      myHT.Add( "three", "brown" );
      myHT.Add( "four", "fox" );
      myHT.Add( "five", "jumped" );

      // Displays the count and values of the Hashtable.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", myHT.Count );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( myHT );

      // Clears the Hashtable.
      myHT.Clear();

      // Displays the count and values of the Hashtable.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", myHT.Count );
      Console.WriteLine( "   Values:" );
      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.

Initially,
   Count    : 5
   Values:
    -KEY-    -VALUE-
    five:    jumped
    three:    brown
    four:    fox
    two:    quick
    one:    The

After Clear,
   Count    : 0
   Values:
    -KEY-    -VALUE-
*/ 

[C++] 
#using <system.dll>
#using <mscorlib.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"one", S"The" );
   myHT->Add( S"two", S"quick" );
   myHT->Add( S"three", S"brown" );
   myHT->Add( S"four", S"fox" );
   myHT->Add( S"five", S"jumped" );

   // Displays the count and values of the Hashtable.
   Console::WriteLine( S"Initially," );
   Console::WriteLine( S"   Count    : {0}", __box(myHT->Count) );
   Console::WriteLine( S"   Values:" );
   PrintKeysAndValues( myHT );

   // Clears the Hashtable.
   myHT->Clear();

   // Displays the count and values of the Hashtable.
   Console::WriteLine( S"After Clear," );
   Console::WriteLine( S"   Count    : {0}", __box(myHT->Count) );
   Console::WriteLine( S"   Values:" );
   PrintKeysAndValues( myHT );
}


 /*
 This code produces the following output.

 Initially,
    Count    : 5
    Values:
     -KEY-    -VALUE-
     five:    jumped
     three:    brown
     four:    fox
     two:    quick
     one:    The

 After Clear,
    Count    : 0
    Values:
     -KEY-    -VALUE-
 */

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

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("one", "The")
myHT.Add("two", "quick")
myHT.Add("three", "brown")
myHT.Add("four", "fox")
myHT.Add("five", "jumped")

// Displays the count and values of the Hashtable.
Console.WriteLine("Initially,")
Console.WriteLine("   Count    : {0}", myHT.Count)
Console.WriteLine("   Values:")
PrintKeysAndValues(myHT)

// Clears the Hashtable.
myHT.Clear()

// Displays the count and values of the Hashtable.
Console.WriteLine("After Clear,")
Console.WriteLine("   Count    : {0}", myHT.Count)
Console.WriteLine("   Values:")
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.
// 
// Initially,
//    Count    : 5
//    Values:
//     -KEY-    -VALUE-
//     five:    jumped
//     three:   brown
//     four:    fox
//     two:     quick
//     one:     The
// 
// After Clear,
//    Count    : 0
//    Values:
//     -KEY-    -VALUE- 

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 | IDictionary.Clear

Show: