Dictionaries are Generic Collections that allow you to specify the type for not only the value stored as an item in the Collection, but also the type used for the key that refers to that item’s place within the Collection.
Take, for instance, an ordinary List<string> which has an implied integer index that can be used to refer to each string item within the List. A Dictionary’s key is very similar to the index used in the List except that it doesn’t have to be an integer, but rather can be of any type (although, it is typically of type int or string).
This can make things easier when trying to find a particular item in a large set of items, and all you have to do is reference its key rather than iterate though the entire Collection looking for an index.
For example, you might have a bunch of cars that you want to store in a Collection and be able to easily retrieve a car by its VIN number. Using a Dictionary, you can use the VIN number as the key and the object’s class type as the value, for example:
The format for a Dictionary collection is to declare a key that corresponds to a type (string, int, etc.) and give it a value type (of type Car, int, etc.) Here we’re using a string as the key to hold the VIN number which can be used to refer to a particular Car object (in other words, the key and the value are “paired” together). Here’s one way that we can add a Car, with a VIN as the key, into the Dictionary Collection:
Notice in the Add() method, you first pass in the key, then treat it in the same way you would a List collection. In addition to this, you can initialize the Dictionary just as you would a list, by doing the following:
Although you are now using a key instead of an array-like indexer, you can still iterate through the Dictionary by using the ElementAt() method and passing an argument that behaves like an ordinary indexer. The code below shows this in action, as well as printing out the key and value for each element:
Notice the different properties used, such as .Key and .Value. The .Key property holds the value of the Key that was determined for each index in the collection, whether a string, integer or anything else. The .Value Property, on the other hand, holds the value that the Key corresponds to. These properties exist to differentiate between the two parts of the Dictionary.
If you want to simply retrieve a single value out of the Collection you can use the TryGetValue() method which will return a value using the specified key and then output it to an existing variable:
What this code block does is create a local variable of type Car called v2, then in the if() statement, performs a check to ensure that a value exists for the key "V2". If it does not, it will exit out of the operation. However, if it does exist, the value will be stored in the v2 variable we created earlier. Then, the result is set to that, using the helper method we created.
To remove an element in the Collection you can call the Remove() method and since it returns a bool -depending on if the element was successfully removed - you can wrap it in a conditional as follows:
This code checks to ensure that an entry exists in the Dictionary that corresponds to the Key "V1". If so, it uses the Remove() method to remove that value from the Dictionary. So that we can see that it successfully completed the operation, we print out a message in the resultLabel:
Lesson 49 - Working with the Dictionary Collection