In c#, Dictionary is a generic type of collection, and it is used to store a collection of key/value pairs organized based on the key. The dictionary in c# will allow to store only the strongly-typed objects, i.e., the key/value pairs of the specified data type.
In c#, while storing the elements in the dictionary object, you need to make sure that the keys are unique because the dictionary object will allow us to store duplicate values, but the keys must be unique.
The size of the dictionary object will vary dynamically so that you can add or remove elements from the dictionary based on our requirements.
In c#, the dictionary object is same as the hashtable object, but the only difference is the dictionary object is used to store a key-value pair of same data type elements.
In c#, the dictionary is a generic type of collection, and it is provided by System.Collections.Generic namespace.
As discussed, the collection is a class, so to define a dictionary, you need to declare an instance of a dictionary class before performing any operations such as add, delete, etc. like as shown below.
If you observe the above dictionary declaration, we created a generic dictionary (dct) with an instance of dictionary class using type parameters (TKey, TValue) as placeholders with angle (<>) brackets.
Here, the angle (<>) brackets will indicate that the dictionary is a generic type and type parameter TKey represents a type of keys to be accepted by the dictionary, and TValue is used to represent a type of values to be accepted by the dictionary.
In c#, the generic dictionary (Dictionary<T>) is an implementation of IDictionary<TKey, TValue> interface, so we can also use IDictionary<TKey, TValue> interface to create an object of the generic dictionary (Dictionary<TKey, TValue>) like as shown below.
Following is the example of initializing a generic dictionary by specifying a required type for key and value.
If you observe the above example, we defined a dictionary (dct) with the required key and value types to store. Here, the dictionary object will store a key of string type and value of int type.
The following are some of the commonly used properties of dictionary objects in the c# programming language.
Property | Description |
---|---|
Count | It is used to get the number of key/value pair elements in the dictionary. |
Item[TKey] | It is used to get or set the value associated with the specified key. |
Keys | It is used to get a collection of keys in the dictionary. |
Values | It is used to get a collection of values in the dictionary. |
The following are some of the commonly used methods of the generic dictionary to perform add, search, insert, delete or sort operations in the c# programming language.
Method | Description |
---|---|
Add | It is used to add elements to the dictionary object with a specified key and value. |
Clear | It will remove all the keys and values from the Dictionary<TKey, TValue>. |
ContainsKey | It is used to determine whether the specified key exists in Dictionary<TKey, TValue> or not. |
ContainsValue | It is used to determine whether the specified value exists in Dictionary<TKey, TValue> or not. |
Remove | It is used to remove an element from Dictionary<TKey, TValue> with the specified key. |
TryGetValue | It is used to get the value associated with the specified key. |
As discussed, while adding elements to the dictionary object, we need to make sure that there will not be any duplicate keys.
Following is the example of adding key/value pair elements to dictionary objects in different ways.
If you observe the above example, we are able to define a new generic dictionary (dct, dct2) collections by using System.Collections.Generic namespace. Here, we added only the defined data type keys and values to the newly created dictionaries (dct, dct2) in different ways.
As discussed, the Add method will throw an exception if we try to add a key (2) that is already existing, so to handle that exception, we used a try-catch block.
When you execute the above c# program, you will get the result as shown below.
If you observe the above result, we got an exception when we tried to add a key (2) that is already existing and added a key (5) that is not existing in the dictionary.
In c#, we have different ways to access dictionary elements, i.e., either using index positions or by iterating through the list using for / foreach loops.
Following is the example of accessing dictionary elements in different ways.
If you observe the above example, we are accessing dictionary elements in different ways by using keys and foreach loops based on our requirements.
When you execute the above c# program, you will get the result as shown below.
In c#, by using the Remove() method, we can delete a key/value pair from the dictionary object.
Following is the example of deleting elements from the dictionary object in the c# programming language.
If you observe the above example, we used a Remove() method to delete a particular key of elements from the dictionary.
When you execute the above c# program, we will get the result as shown below.
By using ContainsKey() and ContainsValue() methods, we can check whether the specified key / value element exists in dictionary or not. In case if it exists, these methods will return true otherwise false.
Following is the example of using ContainsKey() and ContainsValue() methods to check for an item that exists in a dictionary or not in c#.
If you observe the above example, we used a ContainsKey() and ContainsValue() methods to check for particular keys and values that exist in the dictionary (dct) or not.
When you execute the above c# program, you will get the result as shown below.
Contains Key 2: True
Contains Value 'Tutlane': False
The following are the important points that need to remember about the dictionary in c#.
The following are the main differences between hashtable and dictionary in c# programming language.