Unity Learn home
View Tutorial Content
Steps

Lists and Dictionaries

Tutorial
Intermediate
+10 XP
5 Mins
(537)
Summary
How to create and use the List and Dictionary collections.
Select your Unity version
Last updated: November 01, 2022
2021.3
2019.3
2019.2
2019.1
2018.4
2018.3
2018.2
2018.1
2017.4
2017.3
2017.2
2017.1
5.x
4.x
Language
English
Also included in

1.Overview

There are a number of different ways to store collections of data. Each of these ways has various strengths, weaknesses and use cases. This tutorial will briefly cover the basics of two of them: Lists and Dictionaries.

2.Before you begin

New to Unity?

If you’re new to Unity, welcome! The Unity Essentials learning pathway has been designed to help you get set up and ready to create in the Unity Editor. We recommend you complete this pathway before continuing with Intermediate Scripting,.

Review the Unity Editor basics

If you need to refresh your memory of the Unity Editor basics, you can take a moment to review Explore the Unity Editor at any time.

3.Commonalities

Both the collections you will learn about in this tutorial are in the System.Collections.Generic namespace. In order to use them and many other different collections you should include the following line at the top of any scripts you wish to use them:
using System.Collections.Generic;
This line is included by default when a new script is created.
Another similarity between both collections is that they are generic classes. Generic classes are ones that include a type as part of their declaration. Generic types are made through the use of angle brackets <> after the name of whatever is using the generic type - in this case the name of the class. Here is an example:
List<int> numbers = new List<int>();
In this example, a new List is being declared. It has been given the generic type int (meaning it will be a list of integers) after the class name List. The generic type is also used as part of the constructor, after the class name but before the parameter list.

4.Lists

Lists are very similar to arrays in that they hold a number of elements of the same type in a sequence. The strength of Lists over arrays is that they have a number of methods which allow you to grow or shrink the size of the List, to change the order of the elements and other useful functions.
Something to be aware of when using Lists is that when they grow in size, they allocate memory. If this commonly occurs, it can lead to performance problems in your projects.
For full details on Lists see the documentation here.

5.Dictionaries

Unlike Lists and arrays, Dictionaries do not have a meaningful order - one element cannot really be said to be before or after another. They have two types and store data in pairs of those types called KeyValuePairs. The first type is that of the key, each key in a Dictionary must be unique. The second type is that of the value, values need not be unique. Here is an example Dictionary:
Dictionary<AudioClip, AudioMetaData> musicDatabase = new Dictionary<AudioClip, AudioMetaData>();
In this example, the dictionary is being used to pair AudioClips with their associated metadata.
One issue with Dictionaries is that they are not serializable in Unity. This means that even public Dictionaries will not appear in the Inspector and will not save between play sessions.
For full details on Dictionaries see the documentation here.

6.Summary

In this tutorial you went through a brief introduction to some common collection types used throughout C#: Lists and Dictionaries. There is far too much information about them to cover everything here and far too many other similar types to cover those here. However, hopefully this will help you get started in exploring other useful parts of C# that you had not previously considered.

Lists and Dictionaries
Lists and Dictionaries
General Tutorial Discussion
0
0
1. Overview
0
0
2. Before you begin
0
0
3. Commonalities
0
0
4. Lists
0
0
5. Dictionaries
0
0
6. Summary
0
0