C# Program to Check a HashTable collection is empty or not

The Hashtable collection in C# is a collection of key-value pairs organized based on the hash code of the key. Each element is a key-value pair with unique keys, and keys cannot be null. Values can be null and duplicated.

In this article, we will explore different methods to check if a Hashtable collection is empty or not.

Syntax

Following is the syntax for checking if a Hashtable is empty using the Count property

if (hashtable.Count == 0) {
   // Hashtable is empty
}

The Count property returns an integer representing the number of key-value pairs

public virtual int Count { get; }

Using Count Property to Check Empty Hashtable

The most straightforward way to check if a Hashtable is empty is by using the Count property. If it returns 0, the Hashtable is empty; otherwise, it contains elements.

Hashtable.Count Property Count = 0 Empty Hashtable Count > 0 Non-Empty Hashtable Check: hashtable.Count == 0 Namespace: System.Collections

Example - Empty Hashtable

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create an empty Hashtable
      Hashtable myTable = new Hashtable();
      
      // Check if hashtable is empty
      int mySize = myTable.Count;
      if(mySize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("The hashtable is not empty. It has {0} item(s)", mySize);
   }
}

The output of the above code is

Hashtable is empty

Example - Non-Empty Hashtable

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create a Hashtable and add elements
      Hashtable myTable = new Hashtable();
      myTable.Add("1", "One");
      myTable.Add("2", "Two");
      myTable.Add("3", "Three");
      
      // Check if hashtable is empty
      int mySize = myTable.Count;
      if(mySize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("The hashtable is not empty. It has {0} item(s)", mySize);
   }
}

The output of the above code is

The hashtable is not empty. It has 3 item(s)

Using Helper Method for Reusability

You can create a helper method to check if a Hashtable is empty, making your code more readable and reusable

using System;
using System.Collections;

class Program {
   // Helper method to check if Hashtable is empty
   public static bool IsHashtableEmpty(Hashtable table) {
      return table.Count == 0;
   }
   
   public static void Main() {
      Hashtable langCodes = new Hashtable();
      
      // Check empty hashtable
      Console.WriteLine("Empty check: " + IsHashtableEmpty(langCodes));
      
      // Add an element
      langCodes.Add("C#", "CSharp");
      langCodes.Add("Java", "Java Programming");
      
      // Check again
      Console.WriteLine("After adding elements: " + IsHashtableEmpty(langCodes));
      Console.WriteLine("Total elements: " + langCodes.Count);
   }
}

The output of the above code is

Empty check: True
After adding elements: False
Total elements: 2

Key Points

Property Description
Count Property Returns the number of key-value pairs in the Hashtable
Return Type int (32-bit integer)
Empty Condition Count == 0 means the Hashtable is empty
Performance O(1) - constant time operation

Conclusion

Checking if a Hashtable is empty in C# is straightforward using the Count property. When Count returns 0, the Hashtable is empty; any value greater than 0 indicates the presence of elements. This approach provides an efficient O(1) time complexity solution for determining Hashtable emptiness.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements