Checking for key already in dictionary - data reduction
The ConstainsKey method is the way to know if a key already exists in the Dictionary.
This come in handy for data reduction. In the sample below, each time we encountner a new word, we add it as a key in the dictionary, else we increment the counter for this specific word.
XML reduction example : getting all the child nodes names and occurence in an branch of an XML document
Create a dictionary filled with values
This creates a dictionary and immediately fills it with three KeyValuePairs.
You can also add new values later on by using the Add method:
Note that the key (the first parameter) needs to be unique in the dictionary, otherwise an Exception will be thrown.
Getting a dictionary value
You can get the value of an entry in the dictionary using the 'Item' property:
If the key is not present in the dictionary, a KeyNotFoundException will be thrown.
Loop through a dictionary and print all entries
Each pair in the dictionary is an instance of KeyValuePair with the same type parameters as the Dictionary. When you loop through the dictionary with For Each, each iteration will give you one of the Key-Value Pairs stored in the dictionary.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0