Hashtable.Clear Method
.NET Framework 2.0
Removes all elements from the Hashtable.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
Count is set to zero, and references to other objects from elements of the collection are also released. The capacity remains unchanged.
This method is an O(n) operation, where n is Count.
The following example shows how to clear the values of the Hashtable.
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 myHT ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); foreach ( DictionaryEntry de in myHT ) Console.WriteLine( "\t{0}:\t{1}", de.Key, de.Value ); Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Values: -KEY- -VALUE- two: quick three: brown four: fox five: jumped one: The After Clear, Count : 0 Values: -KEY- -VALUE- */
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("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}",
System.Convert.ToString(myHT.get_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}",
System.Convert.ToString(myHT.get_Count()));
Console.WriteLine(" Values:");
PrintKeysAndValues(myHT);
} //main
public static void PrintKeysAndValues(Hashtable myHT)
{
Console.WriteLine("\t-KEY-\t-VALUE-");
IEnumerator myEnumerator = myHT.GetEnumerator();
while (myEnumerator.MoveNext()) {
DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current();
Console.WriteLine("\t{0}:\t{1}", de.get_Key(), de.get_Value());
}
Console.WriteLine();
} //PrintKeysAndValues
} //SamplesHashtable
/*
This code produces the following output.
Initially,
Count : 5
Values:
-KEY- -VALUE-
two: quick
three: brown
four: fox
five: jumped
one: The
After Clear,
Count : 0
Values:
-KEY- -VALUE-
*/
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-
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.