C# Program to Print the Length of the Hashtable


Hashtable collection in C# is a collection of elements wherein each element consist of key-value pair. The key of the element is unique and non-null while the value of the element can be duplicated or even null. The key-and-value pairs are organized based on the hash code of the key.

The key is used to access the elements in the collection. In C#, the class named Hashtable represents the hashtable collection. This class provides various constructors to construct/create the hashtable object. The Hashtable class also provides various methods and properties using which we can manipulate the hashtable collection.

Let’s discuss how we can find out the length of the hashtable collection in C# and then print it to the screen.

How to Print the Length of the Hashtable?

The Hashtable class in C# does not provide a size or length property/function that returns the size/length of the hashtable unlike in Java. So, we work around this limitation and count the number of elements present in the hashtable. This total number of elements in the hashtable is the length of the hashtable.

The counting of elements present in the hashtable is simplified by the Count property the Hashtable class provides. The Hashtable.Count property returns the total number of the key-value pairs present in the hashtable.

Let’s see the prototype of the Count property.

Syntax

public virtual int Count { get; }

Description

Gets the number of elements (key-value pairs) contained in the Hashtable.

Namespace

System.Collections

Property Value

Int32 - The number of key-value pairs present in the hashtable collection.

Implements

Count

As shown in the above prototype, the Count property returns the number of elements (key-value pairs) present in the hashtable. This count is regarded as the length or size of the hashtable.

Let’s now move on to a few programming examples to determine the length of the hashtables.

Example

The first programming example we are going to discuss is given below.

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

In this program, we have hashtable ‘myNumbers’ consisting of numbers as keys and their corresponding number names as values. Then we execute the statement that returns the count of elements in the hashtable, myNumbers.Count.

Output

This property returns the number of elements in the hashtable as shown below.

Length of the hashtable = 5

Since there are five elements in the hashtable, the length of the hashtable is five.

Now, comment out the following code.

myNumbers.Add("10", "Ten");
myNumbers.Add("20", "Twenty");
myNumbers.Add("30", "Thirty");
myNumbers.Add("40", "Forty");
myNumbers.Add("50", "Fifty");

Output

Here we have just created the hashtable object and commented out the “Add” method statements. This means that now there are no elements in the hashtable. Check the following output generated.

Length of the hashtable = 0

So the program rightly generates the output stating the length of the hashtable =0.

Let’s move on to the second example demonstrating Count property.

Example

The program below shows a hashtable object created. We have not added any elements to it yet.

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

Output

Once the Hashtable object is created, we output the length of the hashtable using myTable.Count property. The output, in this case, is given below.

Length of the Hashtable = 0

Since there are no elements the output is 0.

Now we enter the following code in the above program just after the creation of the hashtable object.

myTable.Add("US", "United States");
myTable.Add("IND", "India");
myTable.Add("XX", "");
myTable.Add("UK","United Kingdom");

Here we have added four elements to the hashtable. Note the fourth element. This element has only the key but the value provided is null.

Output

Next, we execute the program and check the output.

Length of the Hashtable = 4

As seen from the output, the hashtable length is four which also includes the element with the null value.

Conclusion

In this article, we have explained and demonstrated the Hashtable.Count property and how it can be used to determine the length of the hashtable collection. Since no direct method returns the size of the hashtable, we use this Count property to determine the size of the hashtable.

Updated on: 22-Dec-2022

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements