Dictionary in C#
- The dictionary is a generic class that belongs to the System. Collection namespace in. NET.
- The dictionary type in C# allows the user to retrieve a corresponding value very quickly for a specified key value.
- A dictionary can store Keys and Values of any data type. NET.
- It provides a mapping from a set of keys to a set of values.
- Represented as Dictionary<TKey, TValue> pair where TKey represents Key and TValue represents the value associated with the key.
- Each Key in a Dictionary is associated with a corresponding Value.
- Retrieving a value using its key is very fast, close to O(1) because the Dictionary<TKey, TValue> class is implemented as a hash table.
- Each key in the dictionary must be unique and cannot be NULL, but a value corresponding to a key can be NULL and/or a duplicate.
- The capacity of a Dictionary<TKey, TValue> varies according to the number of elements present in the Dictionary.
- As elements are added to a Dictionary<TKey, TValue>, the capacity is automatically increased as required by reallocating the internal array.
- The maximum capacity of a dictionary is up to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
- Each item in the Dictionary can be iterated through KeyValuePair<TKey, TValue> structure representing a value and its key.
- In the case of a multi-user environment, a Dictionary<TKey, TValue> can support multiple readers concurrently until the collection is modified, even though enumerating through a collection is intrinsically not a thread-safe procedure.
- In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration.
- So if you have to iterate through each member of the collection, use a List type, but if have want to look up a value in a collection, then always choose Dictionary.
- Has better performance than any other type if the number of elements is large in a collection.
- Almost the same performance as that of an array, so sometimes an array and dictionary objects are interchangeable.
Methods and Attributes in C#
Declaration in C#
A Dictionary can be declared as follows.
Add() in C#
This method is used to add elements with key-value pairs in the dictionary object.
Reading or iterating through Dictionary element
Since the Dictionary type represents a collection, we can use the foreach loop to go through all the items and read them using the Key and Value properties.
Remove() in C#
This method is used to remove an element from a dictionary object.
Clear() in C#
This method removes all the elements from a dictionary object.
ContainsKey() in C#
This method is used to find a key from the Dictionary and it returns a Boolean value indicating whether that key is found in the collection or not.
ContainsValue() in C#
This method is used to check whether a value exists in the dictionary or not and it returns a Boolean value indicating whether that value is found in the collection or not.
Keys in C#
This attribute returns a collection of keys present in the dictionary object. It returns an object of KeyCollection type.
Values in C#
This attribute returns a collection of values present in the dictionary object. It returns an object of ValueCollection type.
Item in C#
The Item property is used to get and set the value associated with the specified key.
Count in C#
This property is used to count the number of elements present in the dictionary at any time.