Chapter 5. Lists, Arrays, and Dictionaries
In previous chapters, you learned how to declare and use a single variable and its type. Now it's time for something more complex. As you know, we can store a value in a variable. But we can also store more than one value in a single variable. In this chapter, we will be talking about special types of variables that allow us to store many values at once.
In this chapter, we will cover the following topics:
What arrays are and why it is good to use them
Storing data in an array
Retrieving data from an array
Lists are powerful, using collections
List or ArrayList
An introduction to dictionaries
An array stores a sequential collection of values of the same type, in the simplest terms. We can use arrays to store lists of values in a single variable. Imagine we want to store a number of student names. Simple! Just create a few variables and name them student1
, student2
, and so on:
There's nothing wrong with this. We can print and assign new values to them. The problem starts when you don't know how many student names you will be storing. The name variable suggests that it's a changing element. There is a much cleaner way of storing lists of data.
Let's store the same names using a C# array variable type:
As you can see, all the preceding values are stored in a single variable called familyMembers
.
To declare a C# array, you must first say what...
Storing items in the List
Using a List instead of an array can be so much easier to work with in a script. Look at some forum sites related to C# and Unity, and you'll discover that a great deal of programmers simply don't use an array unless they have to; they prefer to use a list. It is up to the developer's preference and task. Let's stick to lists for now.
Here are the basics of why a List is better and easier to use than an array:
An array is of fixed size and unchangeable
The size of a List is adjustable
You can easily add and remove elements from a List
To mimic adding a new element to an array, we would need to create a whole new array with the desired number of elements and then copy the old elements
The first thing to understand is that a List has the ability to store any type of object, just like an array. Also, like an array, we must specify which type of object we want a particular List to store. This means that if you want a List of integers—of the int
type—then you can create a...
Now you are probably thinking, "OK, which one should I use?" There isn't any general rule for this. Arrays and List<T> can serve the same purpose. You can find a lot of additional information online to convince you to use one or the other.
Arrays are generally faster. For what we are doing at this stage, we don't need to worry about processing speeds. Some time from now, however, you might need a bit more speed if your game slows down, so this is good to remember.
List<T> offers great flexibility. You don't need to know the size of the list during declaration. There is a massive list of out-of-the-box operations that you can use with List, so it is my recommendation. Array is faster, List<T> is more flexible.
Retrieving the data from the Array or List<T>
Declaring and storing data in the array or list is very clear to us now. The next thing to learn is how to get stored elements from an array. To get a stored element from the array, write an array variable name followed by square brackets. You must write an int
value within the brackets. That value is called an index. The index is simply a position in the array. So, to get the first element stored in the array, we will write the following code:
Unity will return the data stored in the first place in myArray
. It works exactly the same way as the return
type methods that we discussed in the previous chapter. So, if myArray
stores a string value on index 0
, that string will be returned to the place where you are calling it. Complex? It's not. Let's show you by example.
Note
The index value starts at 0, not 1, so the first element in an array containing 10 elements will be accessible through an index value of 0
and last one through...
We definitely know how to use lists now. We also know how to declare a new list and add, remove, and retrieve elements. Moreover, you have learned that the data stored in List<T> must be of the same type across all elements. Let's throw a little curveball.
ArrayList
is basically List<T> without a specified type of data. This means that we can store whatever objects we want. Storing elements of different types is also possible. ArrayList
is very flexible. Take a look at the following example to understand what ArrayList
can look like:
You have probably noticed that ArrayList
also supports all common operations, such as .Add()
. Lines 12 to 15 add different elements into the array. The first two are of the integer
type, the third is a string
type, and the last one is a GameObject
. All mixed types of elements in one variable!
When using
ArrayList
, you might need to check what type of element is under a specific index to know how to treat it in code. Unity provides a very...
When we talk about collection data, we need to mention Dictionaries. Dictionary is similar to a List. However, instead of accessing a certain element by index value, we use a string called Key.
The Dictionary that you will probably be using most often is called Hashtable
. Feel free to dive into the C# documentation after reading this chapter to discover all the bits of this powerful class.
Here are a few key properties of Hashtable
:
Hashtable
can be resized dynamically like List<T> and ArrayList
Hashtable
can store multiple data types at the same type, like ArrayList
A public member Hashtable
isn't visible in the Unity Inspector panel due to default inspector limitations
I want to make sure that you won't feel confused, so I will go straight to a simple example:
Adding elements into hashtable
must contain a string with the key. The key is necessary for retrieving a specific value. We have mentioned this before but I want to highlight the main difference between ArrayList
...
In this chapter, you learned how to use collections of data. You now know what an Array is, what List<T> is, and how to use Hashtable
. If you haven't fully understood this chapter, I suggest a quick read through it again. In the next chapter, we will move on to something more advanced: loops.