BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Arrays

The twenty-eighth part of the C# Fundamentals tutorial increases our knowledge of data types. The tutorial has reviewed many data types, each storing a single value. This article considers the use of arrays to store many values in a simple structure.

What is an Array?

So far in the C# Fundamentals tutorial we have concentrated on basic data types. In each case, the data type stored a single value. Often it is necessary to group together related values that represent a series of data, tabular data or more complex structures. One way that this can be achieved is by creating an array.

An array is a collection of variables with a common data type. The data type can be any type supported by the .NET framework including strings, numeric types and objects. The items in an array are distinguished from one another by one or more index numbers. This allows easy control of the related values.

The .NET framework implements arrays as objects. This does not mean that the individual elements need to be objects but it does affect the ways that you can manipulate arrays. For example, arrays are instantiated using the new keyword. An array may also be left uninitialised with a null value.

Simple Array Declaration

The most basic type of array is the one-dimensional array. This provides a simple list of values. The method used to declare the array is similar to that of any other variable. However, the data type in the declaration is followed by a pair of square brackets. The following example creates an array of integers.

int[] listItems;

Declared in this manner, the array contains null. We can initialise the array using the new keyword and indicating the number of items that we require in the list:

listItems = new int[10];

As with previous declarations, the two elements can be combined into a single statement:

int[] listItems = new int[10];

Index Numbering

C# always starts its array index numbering with item zero. The index increments with each subsequent item. In the above example, the array includes ten items numbered from zero to nine. Each can be used as an individual variable by specifying the variable name followed by the index number in square brackets. The boundaries of arrays are strictly enforced. Any attempt to read or modify a value outside of the declared range cause an exception.

int[] listItems = new int[3];

listItems[0] = 25;
listItems[1] = listItems[0] * 2;    // listItem[1] = 50
listItems[2] = listItems[1] * 2;    // listItem[2] = 100
listItems[3] = listItems[2] * 2;    // Causes an exception

Initialising an Array

As demonstrated, each element in an array can be accessed individually. This can be tedious when filling an array with a set of values and requires excessive amounts of coding. C# provides a way to initialise all of the array variables in a single statement. The comma-separated list of values is provided within a braces and assigned to the declaration. The array size is set automatically.

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

int value = listItems[0];           // value = 2

Retrieving an Array's Length

It is possible that the size of an array to be processed is unknown. To ensure that the boundaries of the array are not exceeded during processing, you can obtain the array's length using the Length property. When reading a property of an array, the square brackets are omitted.

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

int length = listItems.Length;         // length = 3

Two-Dimensional Arrays

The simple "list" type of array described above is a one-dimensional array; the single dimension being traversed using the index. You can create arrays with more than one dimension for more complex structures. For example, a two-dimensional array can hold a table of values. In the next example, we will create an array to hold the following table of string values. The numbered rows and columns indicate the index numbers for each dimension:

Index0123
0AppleBananaClementineDamson
1ElderberryFigGrapeHuckleberry
2Indian PruneJujubeKiwiLime

The first dimension of the array will traverse the row numbers and the second dimension the columns. To declare such an array the dimension boundaries are comma separated in the square brackets. The same notation is used to access the individual variables.

string[,] table = new string[3,4];

table[0,0] = "Apple";
table[0,1] = "Banana";
table[0,2] = "Clementine";
table[0,3] = "Damson";
table[1,0] = "Elderberry";
table[1,1] = "Fig";

// ...and so on

Initialising a Two-Dimensional Array

With tabular data, the initialisation of the array variables can require many lines of code. Again, an initialisation method is available to minimise coding. Each row of the table is initialised with a comma-separated list of values within braces. The rows are also comma-separated and surrounded by a further pair of braces. The following example shows this in action, initialising the entire table from the previous section.

string[,] table = new string[,]
{
    {"Apple", "Banana", "Clementine", "Damson"},
    {"Elderberry", "Fig", "Grape", "Huckleberry"},
    {"Indian Prune", "Jujube", "Kiwi", "Lime"}
};

string fruit = table[1,3];              // fruit = "Huckleberry"
12 January 2007