What is a Dictionary?

A dictionary is another generic collection similar to an array. However, they don’t have a guaranteed order and neither can you add an item to a specific index.

Dictionaries use key-value pairs and require you to reference the key in order to pull out a value.

You can add a new item to the dictionary with the following syntax

This creates a key-value pair in the dictionary

Notice that the “James” key has been inserted somewhere in the dictionary at no specific index. As mentioned previously we cannot guarantee the index of items in a dictionary collection.

The lack of indexing has some benefits. Adding or fetching an element will always be done in O(1) time. Which is a great improvement on O(n)  time to look up a specific item in an array!