Creating 2D, 3D, and Jagged Arrays in C# 11

Arrays are certainly not unique to C#. In fact, just about every other programming and scripting language preceding the introduction of C# provided support for arrays. An array allows a collection of values of the same type to be stored and accessed via a single variable. Each item is accessed in the array variable through the use of an array index.

C# arrays, whilst useful, have some limitations. Perhaps the most significant limitation is the fact that once an array has been created, it cannot be made larger or smaller to accommodate more or fewer values. More dynamic and flexible collection storage capabilities will be covered starting with the chapter titled C# 11 List Collections.

Creating arrays in C#

A C# array may be created in several different ways. One way is to declare an array without initializing it with any values. The syntax for this is as follows:

type[] arrayname;

In the above example, type represents the type of data to be stored in the array (or example, stringintdecimal etc). The square brackets ([]) indicate that this is the declaration for an array, and arrayname is the name by which the array is to be referred.

For example, we can declare an array of strings called myColors as follows:

string[] myColors;

In this example, we have declared the array but not assigned any values to it. To assign values after an array has been declared, the new statement must be used combined with a comma-separated list of values:

string[] myColors;
myColors = new string[] {"red", "green", "yellow", "orange", "blue"};

An array may also be initialized in the declaration line simply by placing the comma-separated list of values after the declaration:

string[] myColors = {"red", "green", "yellow", "orange", "blue"};

Another option is to declare the size of the array when it is created. For example, to declare an array of size 10, place the size value within the square brackets of the new statement:

myColors = new string[5];

This will reserve the space required for the full array without actually placing any values into the array. Finally, this approach may also be combined with the comma-separated value list (although the number of values must match the size specified):

string[5] myColors = {"red", "green", "yellow", "orange", "blue"};

Declaring multidimensional arrays

Multidimensional arrays are declared by placing commas within the square brackets. For example, to declare a two-dimensional array:

char[,] my2Darray; 

The single comma (,) in the above syntax indicates to C# that this is to a two-dimensional array. A two-dimensional array is initialized as follows:

char[,] my2Darray = 
   {'a', 'b', 'c'}, 
   {'c', 'd', 'e'},
   {'c', 'd', 'e'}

This creates a multidimensional array containing three rows and three columns.

When the array was declared above, the task of inferring the array dimensions was left to the C# compiler. These dimensions may also be specified when the array is declared, for example:

char[,] my2Darray = new char[3,3]
   {'a', 'b', 'c'}, 

The following, on the other hand, declares a three-dimensional array:

int[,,] my3Darray;

The following code will initialize the three-dimensional array:

char[,,] my3Darray =
       { 'a', 'b', 'c' }, 
       { 'c', 'd', 'e' } 
       { 'f', 'g', 'h' }, 
       { 'i', 'j', 'k' } 

A three-dimensional array is essentially an array where each element is a two-dimensional array. In the above declaration, the array consists of two arrays, each of which contains a two-dimensional array, each containing three elements. This translates to an array with three dimensions of 2, 2, and 3.

It is also possible to specify the dimensions when the array is declared as follows:

char[,,] my3Darray = new char[2,2,3] 
       { 'a', 'b', 'c' }, 

Declaring jagged arrays

A jagged array is an array where each element is itself an array. The term jagged is used in this context because the array elements can be of different lengths. A jagged array can be thought of as a 2D array where each array can have a different number of elements.

The following is an example of a declaration for a jagged array designed to hold two-dimensional arrays of variable sizes:

char[][] myJaggedArray; 

The following code declares and initializes the same jagged array:

char[][] myJaggedArray =
    new char[] { 'a', 'b', 'c', 'd', 'e' },
    new char[] { 'f', 'g', 'h', 'i' },
    new char[] { 'j', 'j' },
    new char[] { 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r' }