
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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.
- Related Articles
- Java Program to check if a string is empty or not
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- Python program to check if the string is empty or not
- Golang Program to check the given string is empty or not
- How to check if a Laravel collection is empty?
- Golang program to check a value exists in the hash collection or not
- Check whether a Stack is empty or not in Java
- Check whether a HashSet is empty or not in Java
- MySQL query to check if database is empty or not?
- Golang program to check a given key exists in the hash collection or not
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- How to check an array is empty or not using jQuery?
- How to check if a text field is empty or not in swift?
