In this lesson, we'll be covering two generic collections:
Lists and Dictionaries.
We will also be using some of their methods.
Both work in similar ways to arrays,
but with some important differences.
Let's take a look at the List class first.
A list is like a dynamically sized array.
This means you don't need to know how many
elements it's going to have ahead of time.
Before we go too far, let's quickly
create a class that we can sort in our List.
Since a list is generic, it can store any type.
But for our example, we're going to create a class called "BadGuy".
This way we can have a list of bad guys.
This class will have no inheritance,
two public fields named "name" and "power",
and a constructor to allocate these values.
Now we can start making our List.
The first thing to do when making a list, is to include the
System.Collections.Generic namespace.
We will do this at the top of the script.
Next we declare the List. List is a generic class, so after
any modifiers we put the class name followed by a type that
we want to store in the list. In our case, "BadGuy".
Then we give the list a name, and since List is a class
we call the constructor.
Currently our list is empty, so let's allocate to it.
We do this using the Add() function.
This will add a new element to the end of the list.
The parameter for the Add() function is the object
that is being added to the list.
In our case, we'll call a constructor for the BadGuy class.
Now, let's populate the list.
Items in a list can be accessed in the same way as an array,
using an index. It also has a "count" property
which works the same as the "length" property of arrays.
Lists have functions like RemoveAt() and Insert()
for manual arrangement.
RemoveAt() removes an element from the list at a given index,
and moves all elements above it down a place.
Insert() takes an index and an element,
pushing each element after that index up a place.
One of the most powerful functions of a list is Sort().
This can be used to order a list of a given type
by any variable of that type.
It relies on the Type implementing the IComparable interface.
Let's go back to our BadGuy class and implement it.
First, IComparable is in the System namespace,
so we need to declare that we are using it.
Next we need to say that this class
is implementing the interface.
This looks like inheritance.
We're going to use the generic IComparable,
and its generic type needs to be this class.
Lastly, in order to complete the contract
of the Icomparable interface,
we need to declare a public function called CompareTo(),
which returns an integer and takes
our generic type, BadGuy, as a parameter.
The idea behind the CompareTo() method
is that if the object that it is being called from
is greater than the object taken as a parameter,
then the function returns a positive.
If the object the method is called from
is less than the object taken as a parameter,
then the function returns a negative.
And they're equal, then it returns a 0.
What defines one object being greater than another
is decided by the programmer.
For our function, we first want to check
that the BadGuy passed the function exists.
If it doesn't, then this BadGuy is greater,
and the function should return a positive.
Otherwise, the function returns the difference
between the powers of the two BadGuys.
Thus, it will return a positive if the BadGuy
the method is being called from is greater.
Note that we could have based this result on anything.
The interface only requires that we implement the method.
Now in order to sort the list using this comparison,
we call badguys.Sort().
We'll log the badguys list to the console
to show that they have been sorted.
In order to start fresh with the list
and remove all of the elements, use the Clear() function.
These are by no means all of the functions of the List class.
To cover everything falls out of the scope of this tutorial.
For further information, please see the related information links below.
Dictionaries work similarly to Lists, but instead have two types.
This means that each element makes up a Key Value Pair,
sometimes called a KVP for short.
Dictionaries also differ from lists in their intended use.
A list is generally used in place of an array
where more flexibility or functionality is required.
A Dictionary is used as a collection of value
that can be accessed by one or more keys.
The process for declaring a Dictionary
is very similar to that for a List.
First, include the System.Collections.Generic namespace.
Then declare a variable as before, but with two generic types.
The first type is the key. This is the type that is referenced
in order to access the second type, which is the value.
In our example, we have a key type of "string",
and a value type of BadGuy.
We're using this Dictionary to store different search words
that can be used to identify specific BadGuys.
In our Start() method, we declare two BadGuys.
Then we add them to the Dictionary,
just as we would with a List.
Only this time we are adding data for both the key and the value.
Accessing the value that relates to a key is done very similar to
accessing the element of an array or list.
However, instead of using an index,
which would have no inherent meaning to a Dictionary,
we insert a key into the square brackets.
In the case of this example, we insert a string,
and we have the corresponding BadGuy return.
If a key is provided, but not such key exists
in the Dictionary however, this will throw an exception.
Therefore, if it is not guaranteed that the key exists,
it is best to use the TryGetValue() method.
This method has a parameter of the key's type,
and an out parameter of the value's type.
It returns true if the key passed if the first parameter exists.
While this is a safer method for returning a value from a Dictionary,
it is slightly slower than directly referencing the specific key.
For efficiency, use the key within square brackets,
but only when the specified key
is definitely in the Dictionary.