Dictionary.Count Property In C#

A dictionary holds a defined set of keys and corresponding values. The Count attribute provides the total number of entries within the dictionary. Typically, iterations are unnecessary when working with dictionaries. The Count property offers a swift method to ascertain the quantity of data stored in a dictionary. Entries are instantly retrievable as they are added to the dictionary.

What is count property?

Within C#, the Count property is associated with a collection and is responsible for indicating the quantity of elements present within the collection. This feature offers a convenient method to ascertain the size or quantity of items in a collection without the need for explicit iteration.

Syntax:

It has the following syntax:

Example

public int Count{get;}

Return value:

The count of key-value pairs stored within the dictionary.

Example:

Below is a code snippet showcasing how the count property is employed in dictionaries within the C# programming language.

Filename: CountElements.cs

Example

// Program to implement the count property in the dictionary.
using System; 
using System.Collections.Generic; 

class DicCount { 

	// Driver code 
	public static void Main() 
	{ 

		//A new dictionary with the key/value pair is created
		Dictionary<string, string> dict = 
		new Dictionary<string, string>(); 

		// adding key/value pair to the dictionary
		dict.Add("Afghanistan", "Kabul"); 
		dict.Add("Sri Lanka", "Colombo"); 
		dict.Add("Switzerland", "Berne"); 
		dict.Add("Thailand","Bangkok"); 
		dict.Add("Tunisia","Tunis"); 
		dict.Add("Ukraine","Kiev"); 
		dict.Add("Sweden","Stockholm"); 
		dict.Add("Tajikistan","Dushanbe"); 


		//Print statement to display the 
		//count of elements in a dictionary
		Console.WriteLine("The Total number of key/value pairs"+ 
			" in dict ar: " + dict.Count); 
	} 
}

Output:

Output

The Total number of key/value pairs in dict are: 8

Explanation:

This script defines the utilization of a dictionary containing key-value pairs and a Count attribute that denotes the overall quantity of items within the dictionary. The script initiates a fresh dictionary with key and value parameters set as "string" type. Subsequently, multiple key-value pairs are appended to the dictionary utilizing the Add function. Ultimately, the script employs the Count property to display the quantity of key-value pairs present in the dictionary.

Example 2:

Let's consider another example to demonstrate how the count attribute is utilized in dictionaries in C#.

Filename: CountElements.cs

Example

// Program to implement the count property in the dictionary.
using System; 
using System.Collections.Generic; 

class DicCount { 

	// Driver code 
	public static void Main() 
	{ 

		//A new dictionary with the key/value pair is created
		Dictionary<int, string> dict = 
		new Dictionary<int, string>(); 

		// adding key/value pair to the dictionary
		dict.Add(1, "Apple"); 
		dict.Add(2, "Mango"); 
		dict.Add(3, "Banana"); 
		dict.Add(4, "Pine Apple"); 
		dict.Add(5,"Plum"); 
		dict.Add(6,"Papaya"); 
		dict.Add(7,"Apricot"); 
		dict.Add(8,"Watermelon");
		dict.Add(9,"Mango"); 


		//Print statement to display the 
		//count of elements in a dictionary
		Console.WriteLine("The Total number of key/value pairs"+ 
			" in dict are: " + dict.Count); 
	} 
}

Output:

Output

The Total number of key/value pairs in dict are: 9

Explanation:

The process begins by initializing a fresh dictionary containing various key/value pairs, symbolizing each type of fruit along with its respective description. Subsequently, the Count property is employed to fetch the total count of entries within the dictionary, which is then displayed on the console.

Advantages of using Dictionary.Count Property in C#

There are several advantages of the Dictionary.Count Property in C#. Some main advantages of the Dictionary.Count Property are as follows:

  • Effective: The Count property offers an efficient method to determine the number of key-value pairs in a dictionary. The constant time function is O(1) because the dictionary handles the calculations internally.
  • Convenience: Instead of manually walking through the dictionary and counting the elements, the Count property provides an easy and readable way to access the size of the dictionary.
  • Readable rules: Using the Count property, you can improve your code's readability because it is explicitly intended to retrieve the number of elements. It makes the code more transparent and accessible than manual rebuilding or other methods.
  • Optimized data: The Count property allows developers to optimize their algorithms by utilizing the same internal data structures that dictionaries use. It is especially important for large cells where manual rebuilding may not result in optimal performance.
  • Constant communication: In C#, the count property is identical across all types of collections. Whether working with arrays, lists, dictionaries, or other collection classes, developers can utilize the Count property to provide a way to access the number of elements.
  • Dynamic behavior: The dictionary's Count property sets its current size and updates automatically when elements are added or removed. It ensures that the calculations are always correct and reflect the dictionary's actual state.

Input Required

This code uses input(). Please provide values below: