Dictionary

The dictionary collection can be used in scenarios when we need to maintain uniqueness in values that are being saved. A dictionary collection is comprised of two parts: a key and a value. Together, they are referred to as a key-value pair in .NET. When a dictionary collection is created, it ensures that the key-value is unique and no duplicate keys are present in the collection. Retrieval is also based upon the key, making the operation extremely fast. A dictionary collection is declared by the following index:

Dictionary<int, int> vs = new Dictionary<int, int>();

In the preceding code implementation, we are declaring a dictionary collection where both key and value are of the int format.

Let's right-click on the Dictionary class and click on Go to Definition. On doing this, we will be taken to the definition of the dictionary. Upon doing so, we will realize that it implements several interfaces such as IEnumerable, ICollection<KeyValuePair<Tkey, and TValue>>. Please refer to the following definition of the Dictionary class:

Due to the implementation of the ICollection interface based on KeyValuePair, it ensures that the uniqueness is maintained based on the key.

In the code implementation, we will look at a code example wherein we will implement operations on the Dictionary object:

public static void DictionaryCollectionOperations()
{
Dictionary<int, int> vs = new Dictionary<int, int>();
for (int x = 0; x < 5; x++)
{
KeyValuePair<int, int> pair = new KeyValuePair<int, int>(x, x * 100);
}

foreach(KeyValuePair<int, int> keyValue in vs)
{
Console.WriteLine(keyValue.Key + " " + keyValue.Value);
}
vs.Remove(1);
Console.WriteLine(vs[0]);
vs.Add(5, 500);
Console.WriteLine(vs.Count);
bool hasKey = vs.ContainsKey(4);
bool hasValue = vs.ContainsValue(900);
Console.WriteLine(hasKey);
Console.WriteLine(hasValue);
}

Please note the following in the preceding code implementation:

  • We have declared a dictionary collection variable. We have implemented a for loop that runs five times. In the loop, we are creating KeyValuePair and adding it to the dictionary object. 
  • After we have added elements to the dictionary object, we are looping through the dictionary. We are doing it via a foreach loop on KeyValuePair present in the dictionary. 
  • To remove a particular element from the dictionary, we can use the Remove method. The method takes the input of a key and, based on a key, deletes the respective KeyValuePair present in the dictionary. 
  • To add a particular element in the dictionary, we can use the Add method. The method has two parameters, one of the key and another of the value. 
  • To check whether a particular key is present in the dictionary, we can use the ContainsKey method. The method returns whether the given key exists in the dictionary. If the key exists, it returns true and, in other cases, it returns false
  • To check whether a particular value is present in the dictionary, we can use the ContainsValue method. The method returns whether the given value exists in the dictionary. If the value exists, it returns true and, in other cases, it returns false.

If the preceding code is executed, we get the following output:

Please note the following points in the output of the code:

  • The values (0, 0), (1, 100), (2, 200), (3, 300), and (4, 400) indicate the key-value pairs that are added in the dictionary.
  • 5 indicates the length or number of elements present in the dictionary.
  • True indicates that the dictionary contains KeyValuePair with the key 4.
  • False indicates that the dictionary does not contain KeyValuePair with the value 900

Before we move to the next section, let's try what would happen if we try to add KeyValuePair in the same dictionary in which the key already exists in the dictionary. For the sake of an example, we will add KeyValue of (1, 1000). Please note that the key 1 is already present in the dictionary. When the code is executed, we get the following exception. The exception indicates that if we try to add a value with the same key in the dictionary, it will throw an error:

In the next section, we will go over another set of collection objects: queues and stacks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.137.37