VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-dictionary-class/

⇱ C# Dictionary Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Dictionary Class

Last Updated : 11 Jul, 2025

In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value.

  • In Dictionary, each entry consists of a key and its associated value.
  • It provides constant time complexity for insertion, deletion, and lookups.
  • It provides generic support (which means it can work with any data type for both keys and values.

Example: This example, demonstrates how to create a Dictionary, insert key-value pairs and iterate through them to display the keys and their corresponding values.


Output
key: Geek1, value: 1
key: Geek2, value: 2
key: Geek3, value: 3

Declarartion of Dictionary

In C#, the declaration of Dictionary can be done as:

Dictionary<TKey, TValue> dictionaryName = new Dictionary<TKey, TValue>();

Parameters: 

  • TKey: Denotes the type of the keys in the dictionary.
  • TValue: Denotes the type of the values in the dictionary.

Note: Tkey is the type of the keys and Tvalue is the type of the values

Since the Dictionary<TKey, TValue> is a collection of keys and values, the element type is not the type of the key or the type of the value. Instead, the element type is a KeyValuePair <TKey, TValue> of the key type and the value type.  

Constructors

ConstructorsDescription
Dictionary<TKey, TValue>()Initializes a new instance of the Dictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the default equality comparer for the key type. 
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)Initializes a new instance of the Dictionary<TKey, TValue> class that contains elements copied from the specified IDictionary<TKey, TValue> and uses the default equality comparer for the key type.
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)Initializes a new instance of the Dictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the specified IEqualityComparer<T>.
Dictionary<TKey,TValue>(IEqualityComparer<TKey>)Initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<T>.
Dictionary<TKey,TValue>(Int32)Initializes a new instance of the Dictionary class that is empty, has the specified initial capacity and uses the default equality comparer for the key type.
Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)Initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer<T>.
Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)Initializes a new instance of the Dictionary<TKey,TValue> class with serialized data.


Example: This example demonstrates the count of pairs and displaying all the key-value pairs.


Output
Total no of pairs in Dictionary are: 4

The key-value pairs in Dictionary are: 
Key = 1, Value = C
Key = 2, Value = C++
Key = 3, Value = Java
Key = 4, Value = Python

Properties

PropertiesDescription
ComparerGets the IEqualityComparer<TKey, TValue> that is used to determine equality of keys for the dictionary.
CountGets the number of key/value pairs contained in the Dictionary<TKey, TValue>.
Item[TKey]Gets or sets the value associated with the specified key.
KeysGets a collection containing the keys in the Dictionary<TKey, TValue>.
ValuesGets a collection containing the values in the Dictionary<TKey, TValue>.

Example 1: This program demonstrates how to retrieve and display all the keys from the dictionary.


Output
Total key-value pairs are: 4
Key = 1
Key = 2
Key = 3
Key = 4

Example 2: This program demonstrates how to retrieve and display all the values from the dictionary.


Output
Total key-value pairs are: 4
Value = C
Value = C++
Value = Java
Value = Python

Methods

MethodDescription
Add(TKey, TValue)Adds the specified key and value to the dictionary. 
Clear()Removes all keys and values from the Dictionary<TKey, TValue>.
ContainsKey(TKey)Determines whether the Dictionary<TKey, TValue> contains the specified key.
ContainsValue(TValue)Determines whether the Dictionary<TKey, TValue> contains a specific value.
Equals(Object)Determines whether the specified object is equal to the current object. 
GetEnumerator()Returns an enumerator that iterates through the Dictionary<TKey, TValue> .
GetHashCode()Serves as the default hash function.
GetObjectData(SerializationInfo, StreamingContext)Implements the ISerializable interface and returns the data needed to serialize the Dictionary<TKey,TValue> instance.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
OnDeserialization(Object)Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.
Remove(TKey)Removes the value with the specified key from the Dictionary<TKey, TValue>.
ToString()Returns a string that represents the current object.
TryGetValue(TKey, TValue)Gets the value associated with the specified key.

Example 1: This example demonstrates how to use clear() to remove all the elements from the dictionary.


Output
Total key-value pairs: 4
Total key-value pairs after Clear operation: 0

Example 2: This example demonstrates how to remove entries based on their keys.


Output
Total key-value pairs: 4
Total key-value pairs after removal: 2
Comment

Explore