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

What is an array?

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:

public string student1 = "Greg";
public string student2 = "Kate";
public string student3 = "Adam";
public string student4 = "Mia";

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:

public string[ ] familyMembers = new string[ ]{"Greg", "Kate", "Adam", "Mia"} ;

As you can see, all the preceding values are stored in a single variable called familyMembers.

Declaring an array

To declare a C# array, you must first say what type of data will be stored in the array. As you can see in the preceding example, we are storing strings of characters. After the type, we have an open square bracket and then immediately a closed square bracket [ ]. This will make the variable an actual array. We also need to declare the size of the array. It simply means how many places are there in our variable to be accessed. The minimum code required to declare a variable looks similar to this:

public string[] myArrayName = new string[4];

The array size is set during assignment. As you have learned before, all code after the variable declaration and the equal to sign is an assignment. To assign empty values to all places in the array, simply write the new keyword followed by the type, an open square bracket, a number describing the size of the array, and then a closed square bracket. If you feel confused, give yourself a bit more time. Then you will fully understand why arrays are helpful. Take a look at the following examples of arrays; don't worry about testing how they work yet:

string[ ] familyMembers = new string[]{"John", "Amanda", "Chris", "Amber"} ;

string[ ] carsInTheGarage = new string[] {"VWPassat", "BMW"} ;

int[ ] doorNumbersOnMyStreet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

GameObject[ ] carsInTheScene = GameObject.FindGameObjectsWithTag("car");

As you can see, we can store different types of data as long as the elements in the array are of the same type. You are probably wondering, what is the last example which looks different:

GameObject[ ] carsInTheScene = GameObject.FindGameObjectsWithTag("car");

In fact, we are just declaring the new array variable to store a collection of GameObjects in the scene using the "car" tag. Jump into the Unity scripting documentation and search for GameObject.FindGameObjectsWithTag:

Declaring an array

As you can see, GameObject.FindGameObjectsWithTag is a special built-in Unity function that takes a string parameter (tag) and returns an array of GameObjects using this tag.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset