The Wayback Machine - https://web.archive.org/web/20241008091741/https://www.geeksforgeeks.org/c-sharp-dictionary-count-property/
Open In App

C# | Dictionary.Count Property

Last Updated : 01 Feb, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This property is used to get the number of key/value pairs contained in the Dictionary.

Syntax:

public int Count { get; }

Return Value : The number of key/value pairs contained in the Dictionary.

Below are the programs to illustrate the use of above-discussed property:

Example 1:




// C# code to count the number of 
// key/value pairs in  Dictionary
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Create a new dictionary of
        // strings, with string keys.
        Dictionary<string, string> myDict = 
           new Dictionary<string, string>();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // To get count of key/value
        // pairs in myDict
        Console.WriteLine("Total key/value pairs"+
              " in myDict are : " + myDict.Count);
    }
}


Output:

Total key/value pairs in myDict are : 6

Example 2:




// C# code to count the number of 
// key/value pairs in  Dictionary
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Create a new dictionary of 
        // strings, with string keys.
        Dictionary<int, string> myDict = 
          new Dictionary<int, string>();
  
        // Adding key/value pairs in myDict
        myDict.Add(1, "C");
        myDict.Add(2, "C++");
        myDict.Add(3, "Java");
        myDict.Add(4, "Python");
        myDict.Add(5, "C#");
        myDict.Add(6, "HTML");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs"+
              " in myDict are : " + myDict.Count);
    }
}


Output:

Total key/value pairs in myDict are : 6

Note: Retrieving the value of this property is an O(1) operation.

Reference:



Similar Reads

C# | Dictionary.Item[] Property
This property is used to get or set the value associated with the specified key in the Dictionary. Syntax: public TValue this[TKey key] { get; set; } Here, key is the Key of the value to get or set. Property Value: It is the value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, an
2 min read
C# | Dictionary.Values Property
This property is used to get a collection containing the values in the Dictionary<TKey,TValue>. Syntax: public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Values{ get; } Return Value: This property returns a collection containing the Values in the Dictionary. Below are the programs to illustrate the use of Dictiona
2 min read
C# | Dictionary.Keys Property
This property is used to get a collection containing the keys in the Dictionary. Syntax: public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the Dictionary. Below are the programs to illustrate the use of above-discussed property: Example 1: // C#
2 min read
C# | Dictionary.Add() Method
Dictionary<TKey,TValue>.Add() Method is used to add a specified key and value to the dictionary. Syntax: public void Add (TKey key, TValue value); Parameters: key: It is the key of the element to add. value: It is the value of the element to add. The value can be null for reference types. Exceptions: ArgumentNullException : If the key is null
3 min read
C# | Dictionary.ContainsValue() Method
This method is used to check whether the Dictionary<TKey,TValue> contains a specific value or not. Syntax: public bool ContainsValue (TValue value); Here, the value is the Value to locate in the Dictionary. The value can be null for reference types. Return Value: This method returns true if the Dictionary contains an element with the specifie
2 min read
C# | Dictionary.ContainsKey() Method
This method is used to check whether the Dictionary<TKey,TValue> contains the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.
2 min read
C# | Dictionary.Remove Method
This method is used to remove the value with the specified key from the Dictionary<TKey,TValue>. Syntax: public bool Remove (TKey key); Return Value: This method returns true if the element is successfully found and removed; otherwise it returns false. This method returns false if key is not found in the Dictionary. Exception: This method wil
2 min read
C# | Dictionary.Clear Method
This method is used to remove all key/value pairs from the Dictionary<TKey,TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of above-discussed method: Example 1: // C# code to remove all pairs // from Dictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Mai
2 min read
C# | Get an enumerator that iterates through the Dictionary
Dictionary<TKey, TValue>.GetEnumerator method is used to return an enumerator that iterates through the Dictionary<TKey, TValue>. Syntax: public System.Collections.Generic.Dictionary<TKey,TValue>.Enumerator GetEnumerator (); Return Value: This method returns an Dictionary<TKey, TValue>.Enumerator structure for the Dictionary
3 min read
Check if two Dictionary objects are equal in C#
Equals(Object) Method which is inherited from the Object class is used to check if a specified Dictionary object is equal to another Dictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return Value: This method return true if the specified object is equa
2 min read
C# | Dictionary Class
The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a generic collection class in the System.Collections.Generic namespace. The Dictionary <TKey, TValue> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retri
7 min read
Difference between Hashtable and Dictionary in C#
In C#, Dictionary is a generic collection which is generally used to store key/value pairs. Dictionary is defined under System.Collection.Generics namespace. It is dynamic in nature means the size of the dictionary is growing according to the need. Example: [GFGTABS] CSharp // C# program to illustrate Dictionary using System; using System.Collectio
3 min read
C# Dictionary with examples
In C#, Dictionary is a generic collection which is generally used to store key/value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of Dictionary is, it is generic type. Dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature means the size of the dictionary is growing
5 min read
C# | SortedDictionary.Count Property
This property is used to get the number of key/value pairs contained in the SortedDictionary<TKey, TValue>. Syntax: public int Count { get; } Return Value : It returns the number of key/value pairs contained in the SortedDictionary. Below are the programs to illustrate the use of above-discussed property: Example 1: // C# code to count the nu
2 min read
Queue.Count Property in C#
This property is used to get the number of elements contained in the Queue. Retrieving the value of this property is an O(1) operation and it comes under the System.Collections namespace. Syntax: public virtual int Count { get; } Property Value: This property returns the number of elements contained in the Queue. Below programs illustrate the use o
2 min read
Stack.Count Property in C#
This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to Count. Retrieving the value of this property is an
2 min read
StringBuilder.Chars[] Property in C#
StringBuilder.Chars[Int32] Property is used to get or set the character at the specified character position in this instance. Syntax: public char this[int index] { get; set; } Here, the index is the position of the character. Property Value: This property returns the Unicode character at position index. Exceptions: ArgumentOutOfRangeException: If t
2 min read
Thread.CurrentThread Property in C#
A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as CurrentThread to check the current running thread. Or in other words, the value of this property indicates the current running thread. Syntax: public static Thread CurrentThread { get; } Return Value: This property returns a
2 min read
C# | OrderedDictionary.Item[Object] Property
There are two methods in the overload list of this property as follows: Item[Int32] : Used to get or set the value at the specified index.Item[Object] : Used to get or set the value with the specified key. OrderedDictionary.Item[Int32] Property This property is used to get or set the value at the specified index.Syntax: public object this[int index
3 min read
C# | LongLength property of an Array
Array.LongLength Property is used to get a 64-bit integer that represents the total number of elements in all the dimensions of the Array. Syntax: public long LongLength { get; } Property Value: This property returns a 64-bit integer that represents the total number of elements in all the dimensions of the Array. Example: // C# program to illustrat
2 min read
C# | SortedDictionary.Item[] Property
This property is used to get or set the value associated with the specified key. Syntax: public TValue this[TKey key] { get; set; } Here, key is the Key of the value to get or set. Property Value: The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation create
2 min read
C# | SortedDictionary.Keys Property
This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs to illustrate the use of the above-discussed prope
2 min read
C# | SortedDictionary.Values Property
This property is used to get a collection containing the values in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.ValueCollection Values { get; } Return Value: It returns a collection containing the values in the SortedDictionary. Below are the programs to illustrate the use of the above-discusse
2 min read
Queue.IsSynchronized Property in C#
This property is used get a value which indicates whether access to the Queue is synchronized (thread safe) or not. Syntax: public virtual bool IsSynchronized { get; } Property Value: This property returns true if access to the Queue is synchronized(thread safe) otherwise, false. The default is false. Below programs illustrate the use of the above-
2 min read
Stack.IsSynchronized Property in C#
This method(comes under System.Collections namespace) is used to get a value indicating whether access to the Stack is synchronized (thread safe) or not. To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method. Also, retrieving the value of this property is an O(1) operation.
2 min read
Console.KeyAvailable() Property in C#
Console.KeyAvailable Property is used to get a value which shows whether a key press is available in the input stream. This property does not block input until a key press is available. Syntax: public static bool KeyAvailable { get; } Property Value: It will return true if a key press is available otherwise, it returns false. Exceptions: InvalidOpe
1 min read
Console.TreatControlCAsInput Property in C# with examples
Console.TreatControlCAsInput Property is used to get or set a value indicating whether the combination of the Control modifier key and C console key (Ctrl+C) is treated as ordinary input or as an interruption that is handled by the operating system. Syntax: public static bool TreatControlCAsInput { get; set; } Property Value: This property returns
3 min read
How to use AcceptsReturn Property of TextBox in C#?
In TextBox, you are allowed to set a value which shows whether pressing the ENTER in a multiline TextBox control creates a new line of text in the TextBox control or activates the default button for the form by using AcceptsReturn property. If the value of this property is set to be true, then the ENTER key creates a new line of text in a multiline
3 min read
How to use AutoCheck property of CheckBox in C#?
The CheckBox control is the part of the windows form that is used to take input from the user. Or in other words, CheckBox control allows us to select single or multiple elements from the given list. You are allowed to set a value that determines the Checked or CheckedStates values and also the appearance of the CheckBox is set automatically when y
3 min read
Uri.IsLoopback Property in C# with Example
Uri.IsLoopback Property is property of Uri class which used to check that the specified Uri references the local host. Syntax: public bool IsLoopback { get; } Return Value: The return type of this property is Boolean, if this Uri references the local host it returns true otherwise it returns false. Exception: This property will give InvalidOperatio
2 min read