Time for action – create a List of pony names

Create a List that stores the names of some ponies. Since they are names, use the string type.

  1. Modify LearningScript as shown in the next screenshot.
  2. Notice the change on line 2.
  3. Save the file.
  4. In Unity, click on Play.
    Time for action – create a List of pony names

What just happened?

The following screenshot is the Console output. Notice the first output tells you there is a total of 3 elements in the List:

What just happened?

Note

Please notice that your code is using dot syntax, which will be discussed in more detail in the next chapter. The main concepts I want you to focus on here are the features of a List.

The analysis of code is as follows:

  • The code on line 2 is as follows:
    Using System.Collections.Generic;

    To be able to use a List, this tells Unity where to find the necessary C# code files for using a List.

    Change the using statement to using System.Collections.Generic;.

  • The code on line 8 is as fololws:
    List<string> myFavoritePonies = new List<string>();

    This statement creates an empty L ist object.

    First thing to notice is that List<string> specifies that you are creating a List of type string.

    The name of the List is myFavoritePonies.

    Everything on the left side of the assignment operator (=) is creating a variable, declaring the type and the name.

    Everything on the right side is just like assigning a value to a variable, therefore new List<string() is a method called to create a new List object in computer memory, and give that memory location the name of myFavoritePonies.

    Note

    What is different here is that List is an object that itself can store data in elements. Imagine an egg carton as an object that can store the egg type. Creating objects will be discussed more in the next chapter about dot syntax.

  • The code between lines 10 to 12:
    myFavoritePonies.Add("Princess Cadence");

    These three lines of code are adding strings, the pony names, to the myFavoritePonies List.

    Just like an array, each pony name string added is given an index number for the element that each pony name is stored in:

    What just happened?
  • The code on line 14 is as follows:
    Debug.Log("This List has " + myFavoritePonies.Count + " ponies");

    myFavoritePonies.Count retrieves the number of elements in the List.

  • The code between lines 16 and 18:
    Debug.Log("The pony's name at index 1 is " + myFavoritePonies[1]);

    Here you see the index number inside square brackets. Just like an array, this is how to directly retrieve the data in an element at a specific index.

    Like the array, the first element in a List is at index 0.

Have a go hero – add another pony to the List

Add another pony to the List, then display it's name. Also, in the Console, display the number of elements in the List after adding the fourth pony.

Have a go hero – add another pony to the List
Have a go hero – add another pony to the List

Note

Adding an element to the List shows the flexibility it has over an array. This is impossible to do using an array.

Storing items in a Dictionary

A dictionary has a Key/Value pair. The Key is just like an index in an array or list, it's associated with a particular value. The big benefit of a dictionary is that we can specify what the key is going to be. We have to specify the type and the name of the key that will be associated with the value stored.

A real world example you're familiar with is a collection of customers and their ID number. Just by knowing the customer's ID, you could retrieve the customer's information.

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

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