Home >Backend Development >C#.Net Tutorial >C# program to check if HashTable collection is empty

C# program to check if HashTable collection is empty

PHPz
PHPzforward
2023-09-05 11:17:02807browse

检查 HashTable 集合是否为空的 C# 程序

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.

How to check if the 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.

grammar

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.

Namespaces

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.

Example

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.

Output

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.

Example

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);
   }
}

Output

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.

Example

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);
   }
}

Output

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.

Example

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);
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete