The Dictionary<TKey, TValue> collection
A dictionary is a collection of key-value pairs that allows fast lookup based on a key. Adding, searching, and deleting an item are very fast operations and are performed in O(1). The exception here is adding a new value if the capacity must be increased, in which case it becomes O(n).
In .NET, the generic Dictionary<TKey,TValue>
class implements a dictionary. TKey
represents the type of the key and TValue
represents the type of the value. The elements of the dictionary are KeyValuePair<TKey,TValue>
objects.
Dictionary<TKey, TValue>
has several overloaded constructors that allow us to create an empty dictionary or a dictionary filled with some initial values. The default constructor of this class will create an empty dictionary. Take a look at the following code snippet:
var languages = new Dictionary<int, string>();
Here, we are creating an empty dictionary called languages
that has a key of the int...