C# Arrays

In C#, an array is a special type of variable that contains multiple values of the same type. The following string variable, ‘fname’, has been defined and initialised with the name ‘Bob’.

string fname = "Bob";

If multiple names need to be stored, instead of having a different variable for each, an array could be used.

string[] fname = new string[4];
fname[0] = "Bob";
fname[1] = "George";
fname[2] = "Fred";
fname[3] = "Alan";

The first line in the above example declares an array, which holds four string elements. Each element in an array has an index number, which is used in lines two to five above to populate the four elements. Notice that the first index number is a zero and not a one.

The above example can be shortened by declaring and initialising the array in one statement.

string[] fname = { "Bob", "George", "Fred", "Alan" };

As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.


Here, the element with an index number of two would be displayed, in this case “Fred”.

In order to display all of the elements in an array a ‘for’ loop can be used.

for (int i = 0; i < fname.Length; i++)

The above example uses the ‘length’ property of an array to determine the number of elements within it and therefore the number of times to go through the loop. The resulting output will list all the elements of the array on a separate line.


Instead of explicitly stating zero as the first index value for the array and the length for the last, the lower bound and upper bound values of the array can be used to produce the same output.

for (int i = fname.GetLowerBound(0); i <= fname.GetUpperBound(0); i++)

A variation on the ‘for’ loop that can be used to loop through an array is the ‘foreach’ loop, which simplifies the process. The below example produces the same results as above.

foreach (string name in fname)

Although the size of an array needs to be specified when it is defined, it is possible to increase it later. The example below adds space for one extra element to the array.

Array.Resize(ref fname, fname.Length + 1);

Finally, if it is necessary to sort the elements in an array it can be done as follows.


Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, instead of just storing first names in the array, the last names could also be stored.

string[,] names = new string[,] {
    {"Bob", "Smith"},
    {"George", "Jones"},
    {"Fred", "Bloggs"},
    {"Alan", "White"}

This is in effect an array of arrays, with each first name and last name pair enclosed in its own set of curly braces. In order to display the names in the console, nested ‘For’ loops need to be used, with the outer loop cycling through the sets of values, in this case, the first name and last name pairs, whilst the inner loop cycles through the values within each set. Within the inner loop it determines whether it is the first name or the last name by using the index number, with the first name having an index number of zero. If the first name is being displayed, then a space is placed after it, whereas if the last name is being displayed, it creates a new line afterwards for the next name.

for (int nameOuter = names.GetLowerBound(0); 
    nameOuter <= names.GetUpperBound(0); nameOuter++)

    for (int nameInner = names.GetLowerBound(1);
        nameInner <= names.GetUpperBound(1); nameInner++)

        if (nameInner == 0)

            Console.Write(names[nameOuter, nameInner] + " ");


            Console.Write(names[nameOuter, nameInner] + "\n");



The output from this is shown below.

Bob Smith
George Jones
Fred Bloggs
Alan White