C# Tutorial - Dissecting Our Second Application – Arrays


In our application, the Main method takes an array of string objects that are passed in by value as command line arguments. An array is a collection of objects that can be treated as a single item. Arrays come in three flavours; single, multidimensional and jagged. They all derive from the System.Array object, which allows the class methods to be used on your arrays.

Arrays are reference types which are allocated on the managed heap. Any array variable contains a reference to the array and not the array itself. This can be shown in the following example:

string[] myArray;          // Declares a reference to an array
myArray = new string[3];   // Create array of 3 strings

Single Dimension Array

Single-dimensional arrays have a single dimension and can be used to represent a collection of objects. To declare a single-dimension integer array named myArray on a single line we can write:

string[] myArray = new string[3]; // Declare and create array

At this point the array does not actually contain any data, as all of the elements of the array are set to nothing. Therefore we need to populate the elements of the array with some values. This is a good point to mention that arrays are zero indexed, that is to say that the first element of an array is at index zero. So let's populate the array.

myArray[0] = "Adam";
myArray[1] = "Billy";
myArray[2] = "Smith";

We can also declare and initialise the array in a single line to save initialising the array after declaration:

string[] myArray = new string[] {"Adam", "Billy", "Smith"};

This can be shortened to:

string[] myArray = {"Adam", "Billy", "Smith"};

When you use the array to access the elements, you simply use a similar process to that used to populate the array.

string forename = myArray[0];
string middleName = myArray[1];
string surname = myArray[2];

The three string elements would now contain the three strings that are in the array.

Multi Dimensional (Rectangular) Array

A multi-dimensional array has two or more dimensions. For example a multi-dimensional array could be used to represent a grid of information. To declare a multi-dimensional integer array named myIntArray and populate it, we can write:

int[,]myarray = new int[5, 2];

myarray [0, 0] = 0; myarray [0, 1] = 1;
myarray [1, 0] = 2; myarray [1, 1] = 3;
myarray [2, 0] = 4; myarray [2, 1] = 5;
myarray [3, 0] = 6; myarray [3, 1] = 7;
myarray [4, 0] = 8; myarray [4, 1] = 9;

We can also declare and initialise the array in a single line to save initialising the array after declaration:

int[,] myarray = new int[,] {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};

This can be shortened to:

int[,] myarray = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};

Jagged (Staggered) Arrays

Jagged arrays can be used where one or more of the multi-dimensional arrays are not a fixed size. For instance to declare a jagged array of 2 arrays whose sizes are 2 and 4 respectively we would write:

int[][] jaggedarray = new int[2][];

jaggedarray[0] = new int[] {2, 4};
jaggedarray[1] = new int[] {2, 4, 6, 8};

This can be shortened to:

int[][] jaggedarray = { new int[] {2, 4},
                        new int[] {2, 4, 6, 8} };

<< Previous Contents Next >>

© Publicjoe, 2008