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 Dictionary in C#
The Dictionary class in C# provides the Count property to get the number of key/value pairs stored in the dictionary. This property returns an integer representing the total count of elements currently in the dictionary.
Syntax
Following is the syntax for using the Count property −
Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); int count = dictionary.Count;
Parameters
The Count property does not take any parameters. It is a read-only property that returns the current number of key/value pairs in the dictionary.
Return Value
The Count property returns an int value representing the number of key/value pairs contained in the dictionary. It returns 0 for an empty dictionary.
Using Count Property
Example
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
Count with Different Data Types
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "David");
dict.Add(2, "Brian");
dict.Add(3, "Paul");
dict.Add(4, "Ryan");
dict.Add(5, "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
// Remove an element and check count again
dict.Remove(3);
Console.WriteLine("After removing one element: " + dict.Count);
}
}
The output of the above code is −
Count of elements = 5 After removing one element: 4
Count During Dictionary Modifications
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, int> scores = new Dictionary<string, int>();
Console.WriteLine("Initial count: " + scores.Count);
scores["Alice"] = 95;
scores["Bob"] = 87;
Console.WriteLine("After adding 2 elements: " + scores.Count);
scores["Charlie"] = 92;
scores["Alice"] = 98; // Update existing key
Console.WriteLine("After adding 1 more and updating existing: " + scores.Count);
scores.Remove("Bob");
Console.WriteLine("After removing 1 element: " + scores.Count);
}
}
The output of the above code is −
Initial count: 0 After adding 2 elements: 2 After adding 1 more and updating existing: 3 After removing 1 element: 2
Conclusion
The Count property of Dictionary in C# provides an efficient way to determine the number of key/value pairs in the collection. It automatically updates as elements are added, removed, or when the dictionary is cleared, making it useful for tracking dictionary size during operations.
