Dictionaries in Unity

Matthew Clark
Nerd For Tech
Published in
2 min readJun 17, 2022

A dictionary is a generic collection that is used to store key-value pairs. They are similar to arrays but store two components. The first is the key and the second is the value. If you think about it as an English dictionary, the key would be the word and the value would be the definition.

Dictionaries are included using the System.Collections.Generic namespace. To define one, you write it as Dictionary<keytype, objectType>.

To add values to the dictionary you will use the Add() method.

To remove values you will use the Remove() method.

What is unique about dictionaries is that when you search them you will use the key. When the key is found the value will be returned.

An easy way to print both the key and the value is to use the LINQ library. This will allow you to use the ElementAt() method to print both the key and the value.

To print the whole list you can use a foreach loop.

--

--