What is the Count property of Hashtable class in C#?


To find the count of elements of Hashtable class, use the Count property. Firstly, set the Hashtable class with elements −

Hashtable ht = new Hashtable();

ht.Add("One", "Tom");
ht.Add("Two", "Jack");
ht.Add("Three", "Peter");
ht.Add("Four", "Russel");
ht.Add("Five", "Brad");
ht.Add("Six", "Bradley");
ht.Add("Seven", "Steve");
ht.Add("Eight", "David");

Now, count the number of elements using the Count property −

ht.Count

The following is the complete example to learn about the usage of Hashtable Count property in C#.

Example

 Live Demo

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Hashtable ht = new Hashtable();

         ht.Add("One", "Tom");
         ht.Add("Two", "Jack");
         ht.Add("Three", "Peter");
         ht.Add("Four", "Russel");
         ht.Add("Five", "Brad");
         ht.Add("Six", "Bradley");
         ht.Add("Seven", "Steve");
         ht.Add("Eight", "David");

         Console.WriteLine("Count = " + ht.Count);
         Console.ReadKey();
      }
   }
}

Output

Count = 8

Updated on: 20-Jun-2020

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements