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 List that will store only the int type.

Let's go back to the first array example and store the same data in a List. To use a list in C#, you need to add the following line at the beginning of your script:

using System.Collections.Generic;

As you can see, using Lists is slightly different from using arrays. Line 9 is a declaration and assignment of the familyMembers List. When declaring the list, there is a requirement for a type of objects that you will be storing in the list. Simply write the type between the < > characters. In this case, we are using string.

As we are adding the actual elements later in lines 14 to 17, instead of assigning elements in the declaration line, we need to assign an empty List to be stored temporarily in the familyMembers variable. Confused? If so, just take a look at the right-hand side of the equal to sign on line 9. This is how you create a new instance of the list for a given type, string for this example:

new List<string>();
Storing items in the List

Lines 14 to 17 are very simple to understand. Each line adds an object at the end of the List, passing the string value in the parentheses.

Note

In various documentation, Lists of type look like this: List< T >. Here, T stands for the type of data. This simply means that you can insert any type in place of T and the List will become a list of that specific type. From now on, we will be using it.

Common operations with Lists

List<T> is very easy to use. There is a huge list of different operations that you can perform with them. We have already spoken about adding an element at the end of a List. Very briefly, let's look at the common ones that we will be possibly using at later stages:

  • Add: This adds an object at the end of List<T>.
  • Remove: This removes the first occurrence of a specific object from List<T>.
  • Clear: This removes all elements from List<T>.
  • Contains: This determines whether an element is in List<T> or not. It is very useful to check whether an element is stored in the list.
  • Insert: This inserts an element into List<T> at the specified index.
  • ToArray: This copies the elements of List<T> to a new array.

You don't need to understand all of these at this stage. All I want you to know is that there are many out-of-the-box operations that you can use. If you wish to see them all, I encourage you to dive into the C# documentation and search for the List<T> class.

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

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