Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the Count property of Hashtable class in C#?
The Count property of the Hashtable class in C# returns the total number of key-value pairs stored in the hashtable. It is a read-only property that provides an efficient way to determine the size of the collection.
Syntax
Following is the syntax for using the Count property −
int count = hashtable.Count;
Return Value
The Count property returns an int value representing the number of key-value pairs currently stored in the hashtable.
Using Count Property with Basic Operations
The following example demonstrates how to use the Count property to track the number of elements as you add them to a hashtable −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("One", "Tom");
ht.Add("Two", "Jack");
ht.Add("Three", "Peter");
ht.Add("Four", "Russel");
ht.Add("Five", "Brad");
ht.Add("Six", "Bradley");
ht.Add("Seven", "Steve");
ht.Add("Eight", "David");
Console.WriteLine("Count = " + ht.Count);
}
}
The output of the above code is −
Count = 8
Using Count with Dynamic Operations
The Count property automatically updates when elements are added or removed from the hashtable −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
Console.WriteLine("Initial count: " + ht.Count);
ht.Add("A", "Apple");
ht.Add("B", "Banana");
ht.Add("C", "Cherry");
Console.WriteLine("After adding 3 elements: " + ht.Count);
ht.Remove("B");
Console.WriteLine("After removing 1 element: " + ht.Count);
ht.Clear();
Console.WriteLine("After clearing: " + ht.Count);
}
}
The output of the above code is −
Initial count: 0 After adding 3 elements: 3 After removing 1 element: 2 After clearing: 0
Using Count for Validation
The Count property is commonly used to validate whether a hashtable contains elements before performing operations −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable students = new Hashtable();
students.Add("101", "Alice");
students.Add("102", "Bob");
students.Add("103", "Charlie");
if (students.Count > 0) {
Console.WriteLine("Hashtable contains " + students.Count + " students");
Console.WriteLine("Processing student data...");
} else {
Console.WriteLine("No students found in hashtable");
}
Console.WriteLine("Is hashtable empty? " + (students.Count == 0));
}
}
The output of the above code is −
Hashtable contains 3 students Processing student data... Is hashtable empty? False
Conclusion
The Count property of Hashtable class provides an efficient way to determine the number of key-value pairs stored in the collection. It automatically updates as elements are added or removed, making it useful for validation, iteration control, and general collection management operations.
