Key-value pairs can be added, removed, and accessed from dictionaries using both subscript and class methods. To retrieve an element's value, use the subscript operator with the element's key—in the following example, numberOfPotions would be assigned a value of 5:
int numberOfPotions = itemInventory["Potion"];
An element's value can be updated using the same method—the value associated with "Potion" would now be 10:
itemInventory["Potion"] = 10;
Elements can be added to dictionaries in two ways: with the Add method and with the subscript operator. The Add method takes in a key and a value and creates a new key-value element, as long as their types correspond to the dictionary declaration:
itemInventory.Add("Throwing Knife", 3);
If the subscript operator is used to assign a value to a key that doesn't exist in a dictionary, the compiler will automatically add it as a new key-value pair...