C# Arrays

Single-Dimension Array

An array is a set of data items of the same type. Each data item can be accessed using a numerical index. You declare an array as follows:

// Create an array named aBunchOfInts containing 5 elements indexed 0,1,2,3,4
int[] aBunchOfInts = new int[5];

Once defined, you can fill it using the index. (always remember that arrays start with an index of 0).

aBunchOfInts[0] = 5;
aBunchOfInts[1] = 10;
aBunchOfInts[2] = 15;
aBunchOfInts[3] = 20;
aBunchOfInts[4] = 25;

Be aware that if you declare an array but don’t explicitly fill each index, each item will be set to the default value of the data type. (in this example, because its an array containing ints, each int in the array would be a default of 0).

An array can be initialized at the same time it’s being declared like so:

int[] aBunchOfInts = new int[] {5,10,15,20,25};

The var keyword can be used to define an implicitly typed array. this way you can allocate a new array variable without specifying the type contained within the array itself.

// a is an array of ints
var a = new[] {5, 10, 15, 20, 25};

// b is an array of doubles
var b = new[] {2.2, 3.3, 4.4, 5.5, 6.6};

// c is an array of strings
var c  new[] {"hi", "how", "are", "you?"};

Multidimensional Arrays

Rectangular Multidimensional Array

There are two types of multidimensional arrays. The first is a rectangular array, which is an array of multiple dimensions where each row is the same length. The following code initializes a rectangular multidimensional array with 3 rows and 4 columns and then uses a for loop and a nested for loop to fill it with values.

// a rectangular multidimensional array
int[,] myRectangularMDArray;
myRectangularMDArray = new iny[3,4];

// Fill the (3 by 4) array
for(int i = 0; i < 3; i++)
{
     for(int j = 0; j < 4; j++)
     {
          myRectangularMDArray[i, j] = i * j;
     }
}

// Print the (3 by 4) array
for(int i = 0; i < 3; i++)
{
     for(int j = 0; j < 4; j++)
     {
          Console.Write(myRectangularMDArray[i, j] + "\t");
     }
     Console.WriteLine();
}

this example would print out the following to the console window:

0     0     0     0

0     1     2     3

0     2     4     6

Jagged Multidimensional Array

A jagged array contains some number of inner arrays, each of which may have a different upper index limit. The following code initializes a jagged multidimensional array and then fills it with varying length arrays.

// A jagged multidimensional array (i.e., an array of arrays).
// Here we have an array of 5 different arrays.
int[][] myJagArray = new int[5][];

// Create the jagged array.
for (int i = 0; i < myJagArray.Length; i++)
{
     myJagArray[i] = new int[i + 7];
}

// Print each row.
// Because we didn't fill each element with a specific int value, 
// each element will have its default int value which is zero(0)
for (int i = 0; i < 5; i++)
{
     for (int j = 0; j < myJagArray[i].Length; j++)
     {
          Console.Write(myJagArray[i][j] + " ");
     }
     Console.WriteLine();
}

this example would print out the following to the console window:

0000000

00000000

000000000

0000000000

00000000000

Multidimensional arrays take a little getting used to, the nested for loops will take a bit of effort to visualize in your head but once you wrap your head around them they become pretty straight forward.

Leave a Reply

Your email address will not be published. Required fields are marked *