Arrays

Needs more information on the basic description or arrays.
May need some facts checking.

Theory

In C# and many other high-level programming languages, arrays begin to be referenced from zero. This means in an array of ten elements the first one is referenced as zero and the last one is referenced as nine. As arrays are linear peices of information, you cannot declare an array to have elements 5 to 10 and 20 to 30 much in the same way as you cannot have books with pages 5 to 10 and 20 to 30.

Declaring and Populating Arrays

The following block of code demonstrates declaring three different types of arrays. Note that we have not allocated any memory for these arrays, we have merely said that they can be used later on in the application.

// Declare an empty array
string[] playerNames;

To allocate memory for the array we can use the new keyword. In the following example we create room in memory for four empty strings that we then populate with the names of the Pacman ghosts. Note that if we try to reference an element less than zero or higher than the declared size we will get an error while running the application.

// Create a blank array with four elements
playerNames = new string[4];
 
// Fill the variables with month names
monthNames[0] = "Blinky";
monthNames[1] = "Pinky";
monthNames[2] = "Inky";
monthNames[3] = "Clyde";

Inline

If we wanted to save space, or pre-define the contents of the array before hand, you can merge the two examples above to make the following in-line statement:

// Create an array with data
string[] playerNames = new string[4] { "Blinky", "Pinky", "Inky", "Clyde" };

Visualisation

This is how you should visualise a single-dimensional array:

playerNames[0] playerNames[1] playerNames[2] playerNames[3]
Blinky Pinky Inky Clyde

Working with Data

Simple Reference

There are several methods of getting data from arrays, all dependant on your initiali use of them. In some arrays, like the exercise in week eight, you will be merely referencing the information from them like so:

// Outputs: 
//    Inky
string[] playerNames = new string[4] { "Blinky", "Pinky", "Inky", "Clyde" };
Console.WriteLine(playerNames[2]);

Looping through each element

There are other times where you may want to work through each element in an array and perform specific tasks to it. If you know the size of the array you can use a simple for loop statement. If you don't you can find out the length of the array using the property Length as shown in the example below.

// Outputs:
//   1. Blinky
//   2. Pinky
//   3. Inky
//   4. Clyde
string[] playerNames = new string[4] { "Blinky", "Pinky", "Inky", "Clyde" };
 
// Loop through every element of the array
for (int i = 0; i < playerNames.Length; i++)
{
    Console.WriteLine((i + 1) + ". " + playerNames[i]);
}

You can also use the "foreach" construct to loop through each element.

string[] array = { "hello", " ", "world" };
foreach (string str in array)
{
    Console.Write(str);
}

This outputs "Hello World" to the screen. This method is best used when you don't know how long the array is. This only issue is that you can't easily retrieve how far you are through the array.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.