C# | Count the number of key/value pairs in the Hashtable
Last Updated :
01 Feb, 2019
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable.
Syntax:
myTable.Count
Here, myTable is the name of the Hashtable.
Below given are some examples to understand the implementation in a better way:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Hashtable myTable = new Hashtable();
myTable.Add( "2" , "Even & Prime" );
myTable.Add( "3" , "Odd & Prime" );
myTable.Add( "4" , "Even & non-prime" );
myTable.Add( "9" , "Odd & non-prime" );
Console.WriteLine(myTable.Count);
}
}
|
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Hashtable myTable = new Hashtable();
Console.WriteLine(myTable.Count);
}
}
|
Reference:
Share your thoughts in the comments
Please Login to comment...