Declaring an array is similar to other variable types we've worked with, but has a few modifications:
- Array variables require a specified element type, a pair of square brackets, and a unique name.
- The new keyword is used to create the array in memory, followed by the value type and another pair of square brackets.
- The number of elements the array will store goes inside the second pair of square brackets.
In blueprint form, it looks like this:
elementType[] name = new elementType[numberOfElements];
Let's take an example where we need to store the top three high scores in our game:
int[] topPlayerScores = new int[3];
Broken down, topPlayerScores is an array of integers that will store three integer elements. Since we didn't add any initial values, each of the three values in topPlayerScores is 0.
You can assign values directly to an array when it's created by adding them inside a pair of curly brackets at the end of the variable declaration. C# has...