Home >Backend Development >C#.Net Tutorial >C# program to check if HashTable collection is empty
The Hashtable collection in C# is a collection of key-value pairs organized according to the hash code of the key. The hash code is calculated using the hash code function.
Each element in a hash table is a key-value pair with a unique key. The key must also be non-null. Values can be empty and repeated.
In this article, we will discuss how to check if a hash table collection is empty.
The class that implements hash table collection in C# is the Hashtable class. We can check if the hash table collection is empty by counting the number of elements present in the hash table.
To do this, we can use the "Count" property of the Hashtable class, which returns the number of elements in the hash table.
Therefore, if the Count property returns 0, it means that the hash table is empty, and if it returns a value greater than 0, it means that the hash table has elements.
Let’s first take a look at the prototype of the Count property of the Hashtable class.
public virtual int Count { get; }
Return value - Attribute value of type Int32
Description - Gets the number of key-value pairs contained in the hash table.
System.Collections
As can be seen from the above description of the Count attribute, we can use this attribute to obtain the number of key-value pairs in the hash table collection.
Now let's look at some programming examples that will help us understand this Count property.
Let's see how the first program checks whether the hash table is empty. The procedure is as follows.
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 create a Hashtable object but do not add any elements to it. We then use the Count property to retrieve the count of elements present in the hash table. Finally, the value returned by the Count property is calculated and a message is displayed accordingly indicating whether the hash table is empty.
The program generates the following output.
Hashtable is empty
Since there are no elements in the hash table, the message: Hash table is empty is displayed.
Now let us add some elements to the hash table in the above program. Now we add two elements to the hash table using the "Add()" method.
The program is as follows.
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); } }
Here we add two elements to the hash table. The output now changes to look like below.
The hashtable is not empty. It has 2 item(s)
As we can see, the Count property returns the number of elements in the hash table.
Now let us see another example for better understanding.
The procedure is 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); } }
Here we have a langCode hash table with one element in it. We again use the Count property to return the number of elements in the hash table. The output of this program is shown below.
Hashtable is not empty. It has 1 item(s)
Since there is an element in the hash table, the message is given appropriately. Now let us delete the elements present in the hash table. To do this, we simply comment out the line that adds elements to the hash table.
The procedure is 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); } }
There are no elements in the hash table now. So when we use the Count property on this hash table, it returns zero. So the output shows that the hash table is empty.
Hashtable is empty
So, since there is no direct method in the Hashtable class to check if the hash table is empty, we use the Count property of the Hashtable class to get the number of elements in the hash table. If Count returns 0, we conclude that the hash table is empty. If it returns a non-zero value, it means there are elements in the hash table.
The above is the detailed content of C# program to check if HashTable collection is empty. For more information, please follow other related articles on the PHP Chinese website!