Hashtable : Count & Clear
• Count elements in hashtable with Count property
• Clear method will erase all Hashtable contents
• MSDN states that, for Count, "retrieving the value of this property is an O(1) operation.
//using System;
//using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(1, "LoveBot");
hashtable.Add(2, "HateBot");
hashtable.Add(3, "CryBot");
hashtable.Add(10, "EnnuiBot");
//Get Count of Hashtable
//create a count var storing the count of the hashtable instance
int count = hashtable.Count;
Console.WriteLine(count);
//Clear the hashtable
hashtable.Clear();
//Get Count of Hashtable again.
//Should be empty
Console.WriteLine(hashtable.Count);
}
// Output
4
0
} // end Program