Basic C#: Arrays
Objective: Understand what arrays are and how they work in lines of code!
One of the other tools that are commonly used in basic C# scripting is what is known as an array. Let us dive deep into what an array is and how it can be used in the programming world.
- What is an Array?
- Accessing Array Elements
- Arrays in Loops
WHAT IS AN ARRAY?
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
To put it in a simpler way; let us say that we are trying to have a numerous ammount of “int” value variables. Instead of individually coding them one by one, we can store these several variables into one variable itself followed by an array using brackets “[]”. Refer to the below example for a more visualized understanding.
Both ways listed are basic syntax ways of using arrays in our lines of code. If working with unity, we have the ability to access the elements of the array through our inspector view. We can change the size of the array as well as change the element values as opposed to the last way listed as we are naming the values inside our line of code.
NOTE: Arrays can be used across these variable types [Strings, Floats, Ints, Bools (Boolean Arrays)]
ACCESSING ARRAY ELEMENTS
So now that we have a better understanding of what an array is, let’s talk about how we access specific elements stored inside that array through code.
Its quite simple actually. All we need to do is state whatever the name if the variable is followed by the bracket and inside that bracket, we are locating the name of the element. Here is a syntax example:
As an example with unity, I can input a debug.log to print out the element that I am accessing. So after running the application, I get a log message of the element that I am accessing.
One word of friendly advice is that when working with string type arrays, we cannot simply put the name of the element inside the brackets but the number that the name is assigned to it.
For example, if I were to have a string variable, I would not be able to pass in (Debug.Log(name[Cathy]). I would have to look up what that specific number element that string is assigned to and pass that number in instead.
ARRAYS IN LOOPS
One thing that is common in arrays is accessing all its capabilities through loops, whether it for loops or for each loops.
ARRAY IN A FOR LOOP
An example of using a for loop inside an array would be to access all elements at the same time.
So basically I’m stating that for every “i” value that we can access that is listed as an element and is also less than its max element using length, then I basically print out all the elements from 0-max integer.
ARRAY IN A FOR EACH LOOP
We can also use for each loops to print out all elements as well but through a different matter.
For example, I can state that for each (name stored in my array of names, then I can debug.log the name which in this case would be all of them stored. Its as simple as it looks. We use “var” as a universal datatype followed by the name of the datatype.
So both ways work the same exact way but when it comes to working with arrays, I tend to work with for each loops only because its shows a more cleaner syntax than inputting it into a for loop.
NOTE: You can check out how I used a for each loop along with arrays in my article, “Day 48–49: Tracking Enemies Down With the Homing Missile” to get a better understanding of how to use them in actual projects.