TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Count Dictionary

This C# program demonstrates the Count property on the Dictionary type.

Count dictionary. A Dictionary contains a certain number of keys and values.

This number is returned by the Count property. With Count we quickly gauge how much data is stored in the Dictionary. This provides useful metrics about the C# program.

Example. The Count property on Dictionary returns an integer that tells you how many keys are in the Dictionary. This count integer is not equal to the Dictionary fill rate or its internal bucket count.

Int

Instead: Count returns the exact number of key-value pairs you have added and not removed.

C# program that counts keys in Dictionary

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	//
	// Create new Dictionary with four keys.
	//
	Dictionary<string, int> dictionary = new Dictionary<string, int>();
	dictionary.Add("carrot", 1);
	dictionary.Add("pear", 4);
	dictionary.Add("apple", 6);
	dictionary.Add("kiwi", 3);
	//
	// Count the keys.
	//
	int count1 = dictionary.Count;
	//
	// Remove one key.
	//
	dictionary.Remove("pear");
	//
	// Count the keys again.
	//
	int count2 = dictionary.Count;
	//
	// Clear the Dictionary contents.
	//
	dictionary.Clear();
	//
	// Count the keys again.
	//
	int count3 = dictionary.Count;
	//
	// Write the counts of the Dictionary.
	//
	Console.WriteLine(count1);
	Console.WriteLine(count2);
	Console.WriteLine(count3);
    }
}

Output

4
3
0

This program adds four keys to the Dictionary. Its count at this point is equal to 4. Next, one key is removed, resulting in a count of 3. Third, the Dictionary is cleared, which puts its count at zero. The Count property is readonly.

Readonly

Limitations. The Count property gives you no insight into the internal implementation state of the Dictionary. Internally, the Dictionary has many fields, such as a "int[] buckets" field, along with freeList and freeCount fields.

Note: You cannot easily access these members outside of the Visual Studio debugger. This could make performance analysis more difficult.

Condition. The Count property cannot be used to filter or selectively count keys. To count only keys that match some condition, it is easiest to use a foreach-loop over the Keys property, with an if-conditional in the loop.

Also: The Count() extension, from the System.Linq namespace, likely has much worse performance.

Count

Implementation. The Count member on Dictionary is implemented as a property accessor. This means it is not a direct field access. Instead, the call to Count actually does a very simple and fast calculation each time you call it.

Note: The "freeCount" field is subtracted from the "count" field, resulting in one "sub" IL instruction.

Implementation of Count: C#

public int Count
{
    get
    {
	return (this.count - this.freeCount);
    }
}

Summary. We looked at how you can count the number of keys and values in your Dictionary. Because the Dictionary enforces unique keys, you can use Count to compute the number of unique keys in a collection.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf