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.

Multidimensional Arrays

Arrays can contain any number of dimensions. To add extra dimensions you can include extra commas and ranges in the declaration. The initialisation of an array becomes more complex with further nested, comma-separated lists of lists. However, when such complex structures are used, they are generally initialised programmatically, perhaps using information from a database.

int[,,] cube = new int[5, 4, 10];
int[,,,,] fiveDimensional = new int[5, 4, 10, 20, 6];

Retrieving a Multidimensional Array's Size

The size of a multidimensional array can be requested in two ways. The Length property returns the number of individual variables of the entire array. This is equal to the product of the sizes of every dimension.

int[,,] cube = new int[5,4,10];

int length = cube.Length;               // length = 200

The length of a multidimensional array can be a useful number. However, this does not tell us everything we may wish to know about an array. We may need to determine the number of dimensions that an array contains and the length of each dimension. The Rank property returns an integer containing the former. For the latter, the GetLength property is used with an argument indicating the number of the dimension to query.

int[,,] cube = new int[5,4,10];

int dimensions = cube.Rank;             // dimensions = 3
int length0 = cube.GetLength(0);        // length0 = 5
int length1 = cube.GetLength(1);        // length1 = 4
int length2 = cube.GetLength(2);        // length2 = 10

Jagged Arrays

Earlier in the article a two-dimensional array was used to hold a table of data. This type of array is also known as rectangular as it generates a rectangular table. Sometimes it is useful to create a two-dimensional array with rows of varying lengths. Consider the following table:


This table contains three rows but each contains a different number of columns. C# can represent this as a jagged array. A jagged array is actually a nested array of arrays, declared using two pairs of square brackets rather than a single pair with two dimension sizes. Only the number of rows is specified in the initial declaration; the individual rows are sized or initialised individually.

string[][] table = new string[3][];         // Declare the main array

// Declare each sub-array (row lengths)
table[0] = new string[] {"Apple", "Banana", "Clementine"};
table[1] = new string[] {"Cabbage", "Lettuce", "Onion", "Potato"};
table[2] = new string[] {"Cheese", "Milk"};

// Test some values
string fruit = table[0][2];             // fruit = "Clementine"
string dairy = table[2][1];             // dairy = "Milk"

Assigning and Copying Arrays

Arrays are Reference Types

All objects, including arrays, are reference types. This means that the data in the object is stored somewhere in the computer's memory and the variable holds a pointer to that memory. If one object variable is assigned to another, only the pointer to the memory location is duplicated; both variables actually point to the same data. A change in one object's values is therefore reflected in the other object.

This can be demonstrated by assigning one array to another. A change to any of the elements of either array is visible in both:

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

// Point a second array at the same memory range using assignment
int[] secondary = primary;

// Retrieve a value from the primary array
int value = primary[1];                 // value = 4

// Alter the value in the secondary array and re-read the primary
secondary[1] = 99;
value = primary[1];                     // value = 99

It is important to understand the effect of assigning reference types as mistakenly modifying one instance can unexpectedly cause problems elsewhere.

Cloning Arrays

It is often necessary to create a copy of an array that may be manipulated without damaging the contents of the original. To achieve this, the array class includes a Clone method. Rather than assigning a simple reference, this creates a completely new copy of the array. The Clone method returns an object but the specific array type is not specified so this object must be cast to the correct type for assignment.

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

// Clone the array into a secondary array, casting appropriately
int[] secondary = (int[])primary.Clone();

// Retrieve a value from the primary array
int value = primary[1];                 // value = 4

// Alter the value in the secondary array and re-read the primary
secondary[1] = 99;
value = primary[1];                     // value = 4
value = secondary[1];                   // value = 99
12 January 2007