Skip to main content

How to Count the Entries of a Dictionary with Count in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

"How many are there?" is one of the first questions you ask of a Dictionary, whether to show a total or to check that it's empty. In C#, the Count property answers it: the number of key/value pairs the dictionary holds right now.

Syntax​

Syntax
int <count> = <dictionary>.Count;
PartWhat it does
<dictionary>.CountThe number of key/value pairs in the dictionary. It's a property, so there are no brackets after it

Microsoft's Dictionary Count reference: "Gets the number of key/value pairs contained in the Dictionary<TKey,TValue>."

The Example​

A team votes for a treat, one vote per person, kept as person and choice. Ana votes, Ben votes, Ana changes her vote, and Ben's vote is removed. The program prints the count after each step, then a line that depends on it.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints the Count of an empty votes dictionary, sets Ana to Pizza, Ben to Cake and Ana to Cake, prints the Count, removes Ben, prints the Count, and prints No votes yet when Count is 0 and the number of votes otherwise.

main.cs
Dictionary<string, string> votes = [];
Console.WriteLine(votes.Count);

votes["Ana"] = "Pizza";
votes["Ben"] = "Cake";
votes["Ana"] = "Cake";
Console.WriteLine(votes.Count);

votes.Remove("Ben");
Console.WriteLine(votes.Count);

Console.WriteLine(votes.Count == 0 ? "No votes yet" : $"{votes.Count} vote(s)");

votes["Ana"] = ... adds Ana the first time and replaces her vote the second time. The last line uses ? : to pick one of two texts.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
0
2
1
1 vote(s)

A zsh terminal where dotnet run main.cs prints 0, 2, 1 and 1 vote(s).

  1. A new, empty dictionary has a Count of 0.
  2. Three assignments, but only 2 entries: Ana's second vote replaced her first, so she still counts once.
  3. Removing Ben brought it down to 1.
  4. Count == 0 is the check for an empty dictionary.

The command is the same in zsh, bash and PowerShell.

Not Length​

Arrays and strings have Length. A dictionary doesn't:

main.cs
Dictionary<string, string> votes = [];
Console.WriteLine(votes.Length);
Output
/Users/you/demo/main.cs(2,25): error CS1061: 'Dictionary<string, string>' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'Dictionary<string, string>' could be found (are you missing a using directive or an assembly reference?)

The build failed. Fix the build errors and run again.

Counting Only Some Entries​

Count is always the total. To count the entries that match a condition, use LINQ's Count() method, with a condition inside its brackets:

main.cs
Dictionary<string, string> votes = [];
votes["Ana"] = "Cake";
votes["Ben"] = "Pizza";

Console.WriteLine(votes.Count);
Console.WriteLine(votes.Count());
Console.WriteLine(votes.Count(vote => vote.Value == "Cake"));
Output
2
2
1

Count() without a condition gave the same 2 as the property, so for the total, use the property. With vote => vote.Value == "Cake", it counted only the cake votes.

Common Mistakes​

MistakeWhat happensDo this instead
Writing votes.Lengtherror CS1061: 'Dictionary<string, string>' does not contain a definition for 'Length'Use votes.Count
Expecting a replaced key to count twiceSetting an existing key replaces its value, so the count stays the sameCount is one per key
Using Count() for the totalIt works, but the property already holds the numberUse Count, without brackets

Troubleshooting​

SymptomLikely causeFix
CS1061 ... 'Length' on a dictionaryLength belongs to arrays and stringsUse Count
The count is lower than the number of values you setSome of them used a key that was already thereCheck the keys you set

FAQ​

How do I check that a dictionary is empty?​

dictionary.Count == 0, as in Step 1's last line.

Is Count the number of keys or of values?​

Both: each entry is one key with one value, so a dictionary always has as many keys as values.

Conclusion​

dictionary.Count is the number of key/value pairs right now: one per key, whatever happened to its value. Arrays use Length, and LINQ's Count(condition) counts only the entries that match.

Additional Resources​