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
Dictionary.Count Property in C#
The Dictionary.Count property in C# gets the number of key/value pairs contained in the Dictionary<TKey, TValue>. This property is read-only and provides an efficient way to determine the size of your dictionary collection.
Syntax
public int Count { get; }
Return Value
The property returns an int representing the total number of key/value pairs in the dictionary. The count is automatically updated when items are added or removed.
Using Dictionary.Count Property
Basic Usage Example
The following example demonstrates how to use the Count property to track dictionary size −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
Console.WriteLine("Value found!");
else
Console.WriteLine("Value isn't in the dictionary!");
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
foreach(KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Count of elements now = " + dict.Count);
}
}
The output of the above code is −
Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value isn't in the dictionary! Cleared Key/value pairs... Count of elements now = 0
Tracking Count Changes
The following example shows how the count changes as elements are added and removed −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<int, string> dict = new Dictionary<int, string>();
Console.WriteLine("Initial count: " + dict.Count);
dict.Add(1, "Apple");
dict.Add(2, "Banana");
Console.WriteLine("After adding 2 items: " + dict.Count);
dict[3] = "Cherry";
dict[4] = "Date";
Console.WriteLine("After adding 2 more items: " + dict.Count);
dict.Remove(2);
Console.WriteLine("After removing 1 item: " + dict.Count);
dict.Clear();
Console.WriteLine("After clearing all items: " + dict.Count);
}
}
The output of the above code is −
Initial count: 0 After adding 2 items: 2 After adding 2 more items: 4 After removing 1 item: 3 After clearing all items: 0
Common Use Cases
Validation − Check if dictionary is empty before processing
Loop conditions − Use count in for loops or conditional statements
Capacity management − Monitor dictionary size for performance optimization
Progress tracking − Display processing progress based on dictionary size
Conclusion
The Dictionary.Count property provides an efficient O(1) way to get the number of key/value pairs in a dictionary. It automatically updates as items are added or removed and is essential for dictionary size validation and loop operations.
