Arrays and Lists
By the end of this session, you need to be able to:
Explore the data structures of arrays and lists, comparing their functionality and use cases.
Implement arrays and lists in C# in game-like scenarios.
Arrays
So far, we have declared and initialised some variables such as an integer to keep the game’s score:
int score = 0;
Sometimes, we need to make this variable into an array instead of a single variable.
Arrays are a way for a variable to have more than one piece of data, of the same type, attached to it.
That data can then be accessed easily using the variable name + [number] syntax:
enemy[3]
Let’s make an array!
Let’s imagine we are making an RPG game. We want specific XP milestones to make our character level up.
We could do this with an integer array.
Let’s look at some syntax (click on sections to explore):
levelUps =
How do we read arrays?
Once we have declared and initialized our array, we can then read them by typing the name + some number in brackets:
levelUps[0];
levelUps[1];
levelUps[2];
Note - arrays start counting from zero in C# (and many other languages). This is known as a ‘zero-indexed array’.
levelUps[0];
//returns 10
levelUps[1]
; //returns 23
levelUps[2];
//returns 59
What if you don’t want to initialise the array yet?
Sometimes, you want to declare the array but not initialise it straight away (as in, store something in it). We can do this using a slightly different syntax:
scoreThreshold[0] = 4;
scoreThreshold[1] = 9;
etc.
ACTIVITY #1
Work in pairs, ideally a more experienced programmer with a beginner.
Declare and initialise the following in C#.
A string array with all the days of the week.
A float array with 10 values, each representing a point in time (minutes and seconds) when a boss enemy will spawn in a ‘shoot-em-up’.
Use any coding environment you want, including DotNetFiddle
What might arrays be used for?
You might create an array at the start of a combat encounter that tracks and controls all the enemies in the space.
You might save a series of animations or frames in arrays to manage them during combat.
You might make a grid-based game, so the grids themselves are likely to be arrays!
Lists
Arrays are a very efficient way of storing sequential information. However, they are limited by their ability to easily add or remove elements at runtime (when the game is running).
This is where the next data structure, lists, comes into use.
Lists are a more complicated data structure, but can easily be added to and removed from, making them a good option.
To start with, we have to import the C# library that includes lists. We do this at the very top of a C# script:
Line 2 is where we have done this, declaring:
using System.Collections.Generic;
Now, we can add lists to our scripts. The syntax for declaring a script isn’t too different to arrays (click to see details):
scores =
Adding elements to lists
Our list exists, but is empty! We can add elements to list using the .Add() method:
scores.Add(5);
scores.Add(9);
Our list, called scores, has elements added to it now, namely the numbers 5 and 9. The order we add determines their order in the list. 5 is first.
Removing elements from a list
A key feature of a list is the ability to remove elements from it:
scores.Remove(5);
Removes the first instance of the number 5.
scores.RemoveAt(0);
Removes the value at index 0.
scores.Clear();
Removes everything from the list.
Reading elements in the list
Reading the elements can be done with the same syntax as arrays, the name of list, with an index in brackets:
scores[0];
//Returns 5.
scores[1];
//Returns 8.
Note: we have only scratched the surface here of what can be done with both these data structures!
ACTIVITY #2:
Again, work in pairs. One more experienced, one more beginner. Any scripting environment (e.g. DotNetFiddle).
Complete the following:
Create a string list of all the months of the year.
Create an 5-element string list of films you want to see this year. Then, let’s imagine you just saw two of them. Remove them from the list.
Activity 1: Solutions
Activity 2: Solutions
Further Resources:
How to Program in C# - Arrays (E05)
C# List Tutorial - Everything You Need To Learn About List In C#
Last updated