C# Program to Print the Length of the Hashtable

A Hashtable in C# is a collection that stores key-value pairs, where each key is unique and used to access its corresponding value. Unlike arrays or lists, Hashtables don't have a direct Length property, but we can determine the number of elements using the Count property.

The Hashtable class belongs to the System.Collections namespace and organizes elements based on the hash code of their keys. Keys must be unique and non-null, while values can be duplicated or null.

Syntax

The Count property returns the number of key-value pairs in the Hashtable

public virtual int Count { get; }

Using Count Property to Get Hashtable Length

Example with Elements

Here's how to create a Hashtable with elements and print its length

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Creating a Hashtable
      Hashtable myNumbers = new Hashtable();
     
      // Adding elements to Hashtable
      myNumbers.Add("10", "Ten");
      myNumbers.Add("20", "Twenty");
      myNumbers.Add("30", "Thirty");
      myNumbers.Add("40", "Forty");
      myNumbers.Add("50", "Fifty");
 
      // Get the number of key-value pairs
      Console.WriteLine("Length of the hashtable = {0}", myNumbers.Count);
   }
}

The output of the above code is

Length of the hashtable = 5

Example with Empty Hashtable

When no elements are added to the Hashtable, the Count property returns zero

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Creating an empty Hashtable
      Hashtable myTable = new Hashtable();
      Console.WriteLine("Length of the empty Hashtable = {0}", myTable.Count);
   }
}

The output of the above code is

Length of the empty Hashtable = 0

Example with Null Values

Hashtables can contain null values, and these are counted in the total length

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Creating a Hashtable
      Hashtable countries = new Hashtable();
      
      // Adding elements including one with null value
      countries.Add("US", "United States");
      countries.Add("IND", "India");
      countries.Add("UK", "United Kingdom");
      countries.Add("XX", null);  // null value
      
      Console.WriteLine("Length of the Hashtable = {0}", countries.Count);
      
      // Display all key-value pairs
      Console.WriteLine("\nHashtable contents:");
      foreach (DictionaryEntry item in countries) {
         Console.WriteLine("Key: {0}, Value: {1}", 
                          item.Key, 
                          item.Value ?? "null");
      }
   }
}

The output of the above code is

Length of the Hashtable = 4

Hashtable contents:
Key: XX, Value: null
Key: US, Value: United States
Key: UK, Value: United Kingdom
Key: IND, Value: India

Key Points

  • The Count property returns an int representing the total number of key-value pairs.

  • Empty Hashtables have a Count of 0.

  • Elements with null values are included in the count.

  • The Count property is read-only and automatically updates when elements are added or removed.

Comparison with Other Collections

Collection Type Size Property Description
Array Length Gets the total number of elements
List<T> Count Gets the number of elements
Hashtable Count Gets the number of key-value pairs

Conclusion

The Count property of the Hashtable class provides an easy way to determine the number of key-value pairs in the collection. This property automatically reflects the current size of the Hashtable, making it reliable for checking whether the collection is empty or contains elements.

Updated on: 2026-03-17T07:04:36+05:30

888 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements