BlackWaspTM
C# Programming
.NET 1.1+

C# Arrays

by Richard Carr, published at http://www.blackwasp.co.uk/CSharpArrays.aspx

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
12 January 2007