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
Count the number of key/value pairs in HybridDictionary in C#
A HybridDictionary in C# is a collection that automatically switches between a ListDictionary for small collections and a Hashtable for larger collections. To count the number of key/value pairs in a HybridDictionary, we use the Count property.
The HybridDictionary optimizes performance by using different internal storage mechanisms based on the number of elements. For collections with 10 or fewer items, it uses a ListDictionary, and for larger collections, it switches to a Hashtable.
Syntax
Following is the syntax for getting the count of key/value pairs in a HybridDictionary −
HybridDictionary dictionary = new HybridDictionary(); int count = dictionary.Count;
Using Count Property
The Count property returns the total number of key/value pairs currently stored in the HybridDictionary −
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "SUV");
dict1.Add("B", "MUV");
dict1.Add("C", "AUV");
Console.WriteLine("HybridDictionary1 elements...");
foreach(DictionaryEntry d in dict1){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nHybridDictionary2 elements...");
foreach(DictionaryEntry d in dict2){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict2.Count);
dict2.Clear();
Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
}
}
The output of the above code is −
HybridDictionary1 elements... A SUV B MUV C AUV Count of Key/value pairs in Dictionary1 = 3 HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Count of Key/value pairs in Dictionary2 = 6 Count of Key/value pairs in Dictionary2 (Updated) = 0
Count Property with Dynamic Operations
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict = new HybridDictionary();
dict.Add("A", "SUV");
dict.Add("B", "MUV");
dict.Add("C", "AUV");
Console.WriteLine("HybridDictionary elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of Key/value pairs in Dictionary = "+dict.Count);
dict.Add("D", "Utility Vehicle");
dict.Add("E", "Convertible");
Console.WriteLine("Count of Key/value pairs in Dictionary (Updated) = "+dict.Count);
}
}
The output of the above code is −
HybridDictionary elements... A SUV B MUV C AUV Count of Key/value pairs in Dictionary = 3 Count of Key/value pairs in Dictionary (Updated) = 5
Key Features
-
The
Countproperty returns anintrepresenting the total number of key/value pairs. -
The count is automatically updated when items are added or removed from the HybridDictionary.
-
Using
Clear()method sets the count to zero by removing all elements. -
The Count property has O(1) time complexity, making it efficient for frequent count operations.
Conclusion
The Count property of HybridDictionary provides an efficient way to determine the number of key/value pairs in the collection. This property automatically updates as items are added or removed, making it reliable for tracking collection size in real-time applications.
