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
Get the number of key/value pairs in the StringDictionary in C#
The StringDictionary class in C# provides the Count property to get the number of key/value pairs stored in the dictionary. This property is useful for determining the size of the collection and performing operations based on the dictionary's length.
Syntax
Following is the syntax to get the count of key/value pairs in a StringDictionary −
int count = stringDictionary.Count;
Return Value
The Count property returns an integer representing the number of key/value pairs currently stored in the StringDictionary.
Using Count Property with Basic StringDictionary
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "One");
strDict.Add("2", "Two");
strDict.Add("3", "Three");
strDict.Add("4", "Four");
Console.WriteLine("StringDictionary key-value pairs...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of StringDictionary key-value pairs..." + strDict.Count);
}
}
The output of the above code is −
StringDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Count of StringDictionary key-value pairs...4
Using Count with Multiple StringDictionary Objects
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1){
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("Count of StringDictionary1 key-value pairs..." + strDict1.Count);
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of StringDictionary2 key-value pairs..." + strDict2.Count);
}
}
The output of the above code is −
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Count of StringDictionary1 key-value pairs...7 StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Count of StringDictionary2 key-value pairs...5
Checking Empty StringDictionary
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary emptyDict = new StringDictionary();
Console.WriteLine("Empty StringDictionary count: " + emptyDict.Count);
emptyDict.Add("key1", "value1");
Console.WriteLine("After adding one element: " + emptyDict.Count);
emptyDict.Remove("key1");
Console.WriteLine("After removing element: " + emptyDict.Count);
}
}
The output of the above code is −
Empty StringDictionary count: 0 After adding one element: 1 After removing element: 0
Conclusion
The Count property of StringDictionary provides a simple way to determine the number of key/value pairs in the collection. This property is particularly useful for validation, loop control, and determining if the dictionary is empty before performing operations.
