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


The Hashtable collection in C# is a collection of key-value pairs that are organized based on the hash code of the key. The hash code is calculated using a hash code function.

Each element in the hashtable is a key-value pair with unique keys. Keys also have to be nonnull. Values can be null and duplicated.

In this article, we will discuss how to check if the hashtable collection is empty.

How to Check if the Hashtable Collection is Empty?

The class in C# that implements the hashtable collection is the Hashtable class. We can check if the hashtable collection is empty or not by counting the number of elements present in the hashtable.

To do this we can use the “Count” property of the Hashtable class that returns the number of elements in the hashtable.

So if the Count property returns a 0, it will mean the hashtable is empty and if it returns a value greater than 0, the hashtable has elements.

Let’s first understand the prototype of the Count property of the Hashtable class.

Syntax

public virtual int Count { get; }

Return value − Property Value of type Int32

Description − Gets the number of key-value pairs contained in the Hashtable.

Namespace

System.Collections

As evident from the above description of the Count property, we can get the number of key-value pairs in the hashtable collection using this property.

Now let’s see a few programming examples that will help us understand this Count property.

Examples

Let’s see the first program on how to check if the hashtable is empty. The program is given below.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable myTable = new Hashtable();
      //get the count of items in hashtable
      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);
   }   
}

In this program, we have created a Hashtable object and haven’t added any elements to it. Then we retrieve the count of elements present in the hashtable using the Count property. Finally, the value returned by the Count property is evaluated, and accordingly, the message is displayed indicating whether the hashtable is empty or not.

Output

The program generates the following output.

Hashtable is empty

Since there are no elements in the hashtable, the message is displayed that hashtable is empty.

Now let’s add some elements to the hashtable in the above program. Now we are adding two elements to the hashtable using the “Add()” method.

Example

The program looks like this.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable myTable = new Hashtable();
      myTable.Add("1", "One");
      myTable.Add("2", "Two");
      //get the count of items in hashtable
      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);
   }
}

Output

Here we have added two elements to the hashtable. Now the output changes to the one shown below.

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

As we can see, the Count property returned the number of elements in the hashtable.

Now let’s see another example for better understanding.

Example

The program is given below.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable langCode = new Hashtable();
      langCode.Add("Perl", "");
      //get the count of items in hashtable
      int hashtabSize = langCode.Count;
      if(hashtabSize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("Hashtable is not empty. It has {0} item(s)", hashtabSize);
   }
}

Output

Here we have a langCode hashtable with one element in it. Again we use the Count property that returns the number of elements in the hashtable. The output for the program is given below.

Hashtable is not empty. It has 1 item(s)

As the hashtable has one element in it, the message is given appropriately. Now let’s remove the elements present in the hashtable. To do this we simply comment out the lines that add elements to the hashtable.

Example

The program will be as follows.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable langCode = new Hashtable();
      //langCode.Add("Perl", "");
      //get the count of items in hashtable
      int hashtabSize = langCode.Count;
      if(hashtabSize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("Hashtable is not empty. It has {0} item(s)", hashtabSize);
   }
}

Output

Now the hashtable is not having any element in it. So when we use the Count property on this hashtable, it returns zero. Thus the output shows the hashtable is empty.

Hashtable is empty

Thus as there is no direct method in the Hashtable class to check if the hashtable is empty or not, we use the Count property of the Hashtable class to get the count of elements in the hashtable. If the Count returns 0 then we conclude the hashtable is empty. If it returns a non-zero value, this means the hashtable has elements in it.

Updated on: 21-Dec-2022

915 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements