Array in C# – Tutorial

Arrays in c# are powerful and full-featured.

Simple Array in C#

The simple array is a single-dimensional array. The idea of an array is to be a list of items. Lets give you two examples.

Example 1 – Single-dimensional array example with integers

This list of numbers is an array.

1, 2, 3, 4, 5

To create an array in C# with the above list of numbers you would use the following code:

    int[] nums = new int[] { 1, 2, 3, 4, 5 };

The variable “nums” now holds all five number values.  These can be accessed with bracket notation with an index starting at 0. That means that nums[0] would return the first item in the array, which is 1.
nums[4] would return 5.  However, be aware that you cannot overstep your bounds. You only have nums[0] through nums[4] and if you try to access any index out of the range, such as nums[5] you will get an exception.

You can change any item in the array as well.

    nums[1] = 100;

After running the above statement, your array now looks like this:

1, 100, 3, 4, 5

Example 2 – Single-dimensional array example with chars

This list of characters is an array.

‘A’, ‘a’, ‘B’, ‘b’, ‘C’, ‘c’, ‘D’, ‘d’, ‘E’, ‘e’, ‘F’, ‘f’

To create an array in C# with the above list of characters you would use the following code:

    char[] alphas = new char[] { 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f' };

The variable “alphas ” now holds all the listed character values.  These can be accessed with bracket notation with an index starting at 0. That means that alphas[0] would return the first item in the array, which is A. alphas[2] would return B.  alphas[11] would return f.  However, be aware that you cannot overstep your bounds. You only have alphas[0] through alphas[11] and if you try to access any index out of the range, such as alphas[12] you will get an exception.

You can change any item in the array as well.

    alphas[0] = '@';

After running the above statement, your character array now looks like this:

‘@’, ‘a’, ‘B’, ‘b’, ‘C’, ‘c’, ‘D’, ‘d’, ‘E’, ‘e’, ‘F’, ‘f’

Example 3 – Single-dimensional array example with chars

This list of strings is an array.

“apple”, “orange”, “banana”, “peach”, “pear”, “grapefruit”

To create an array in C# with the above list of numbers you would use the following code:

    string[] fruit= new string[] { "apple", "orange", "banana", "peach", "pear" };

The variable “fruit” now holds all the listed string values.  These can be accessed with bracket notation with an index starting at 0. That means that fruit[0] would return the first item in the array, which is “apple”. fruit[2] would return “banana”.  fruit[5] would return “grapefruit”.  However, be aware that you cannot overstep your bounds. You only have fruit[0] through fruit[5] and if you try to access any index out of the range, such as fruit[6] you will get an exception.

You can change any item in the array as well.

    fruit[2] = "plantain'';

Before this statement, fruit[2] was assigned the string “banana”, but after running the above statement, it is assigned the string “plantain”. So now your character array now looks like this:

“apple”, “orange”, “plantain”, “peach”, “pear”

Arrays of Arrays in C#

There are different kinds of Arrays of . Lets discuss them as they are not the same.

  • multidimensional
  • jagged arrays.

With multidimensional arrays, the arrays are arrays of lists, but the lists themselves are not exactly formed into an official C# array object. Some might argue that these are not actually arrays but it that is all semantics.

With jagged arrays, the arrays are arrays of lists, and the lists themselves are also C# array objects.

Multidimensional Array in C#

A multi-dimensional array is an array of fixed size arrays and is often depicted at a table.  Lets learn by some examples.

Two-dimensional array examples

First lest have three examples of two-dimensional arrays.

Example 1 – A multidimensional array of number pairs

Imagine you have the following pairs of numbers:

{ 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 }, { 50, 51 }

The table representation would look like this.

10 11
20 21
30 31
40 41
50 51

You would code this as follows:

    int[,] numgrid = new int[,]{ { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 }, { 50, 51 } };

Since no size was specified the size was automatically determined for you. Alternately you can specify the size:

    int[,] numgrid = new int[5,2]{ { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 }, { 50, 51 } };

You can now access any of the numbers using the following syntax.

    int val = numgrid[0,0];

The above will return the first item in the first pair. Or to get the second item in the last pair do this:

    int val = numgrid[4,1];

However, because this is a multi-dimensional array, you cannot access a pair as array. The following is NOT allowed.

    int[] pair = numgrid[0];

That gives you a compile error. That is because the number pairs are not really stored in memory as C# array objects.

Example 2 – A multidimensional array of three numbers

Imagine you have the following groups of numbers, where the groups are always three numbers.

{ 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 }, { 40, 41, 42 }, { 50, 51, 52 }

The table representation would look like this.

10 11 12
20 21 22
30 31 32
40 41 42
50 51 52

You would code this as follows:

    int[,] threeColumnsOfNumbers = new int[,] { { 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 }, { 40, 41, 42 }, { 50, 51, 52 }  };

Since no size was specified the size was automatically determined for you. Alternately you can specify the size:

    int[,] threeColumnsOfNumbers = new int[5,3] { { 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 }, { 40, 41, 42 }, { 50, 51, 52 }  };

You can now access any of the numbers using the following syntax.

    int val = threeColumnsOfNumbers[0,0];

The above will return the first item in the first group of three. Or to get the last or third item in the last group do this:

    int val = threeColumnsOfNumbers[4,2];

However, because this is a multi-dimensional array, you cannot access a group of three as an array. The following is NOT allowed.

    int[] groupOfThree = threeColumnsOfNumbers[0];

That gives you a compile error. That is because the groups of three are not really stored in memory as C# array objects.

Example 3 – A multidimensional array of characters

Imagine you have the following list of upper and lowercase characters.

{ ‘A’, ‘a’ }, { ‘B’, ‘b’ }, { ‘C’, ‘c’ }, { ‘D’, ‘d’ }, { ‘E’, ‘e’ }, { ‘F’, ‘f’ }

The table representation would look like this.

A a
B b
C c
D d
E e

You would code this as follows:

    char[,] alphaUpperAndLower = new char[,]{ { 'A', 'a' }, { 'B', 'b' }, { 'C', 'c' }, { 'D', 'd' }, { 'E', 'e' }, { 'F', 'f' } };

Since no size was specified the size was automatically determined for you. Alternately you can specify the size:

    char[,] alphaUpperAndLower = new int[6,2]{ { 'A', 'a' }, { 'B', 'b' }, { 'C', 'c' }, { 'D', 'd' }, { 'E', 'e' }, { 'F', 'f' } };

The first digit inside the brackets, 6, indicates  the number of groups. The second digit, 2, indicates the number of items in the group.

You can now access any of the numbers using the following syntax.

    char val = alphaUpperAndLower[0,0];

The above will return the first item in the first pair, which is ‘A’. Or to get the second item in the last pair do this:

    char val = alphaUpperAndLower[5,1];

However, because this is a multi-dimensional array, you cannot access the character pairs as an array. The following is NOT allowed.

    char[] pair = alphaUpperAndLower[0];

That gives you a compile error. That is because the number pairs are not really stored in memory as C# array objects.

Three-dimensional array examples

We have already shown you some two-dimensional examples, now here are some three-dimensional examples.

Note: Of course, there is no limit to the number of dimensions you can create, but each dimension becomes exponentially more complex.

Example 4 – A three-dimensional array of numbers

Imagine you have the following groups of number groups, where the number groups are always three numbers, and there are always three groups.

{ 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 },

{ 40, 41, 42 }, { 50, 51, 52 } , { 60, 61, 62 },

{70, 71, 72 }, { 80, 81, 82 }, { 90, 91, 92 }

So if two-dimensional was displayed as a grid, you can assume that three-dimensional is displays similar to a cube. We have 3 groups of three groups of three numbers or 3x3x3, which is a cube.

You would code this as follows:

int[, ,] cube = new int[,,] { { { 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 } },
                              { { 40, 41, 42 }, { 50, 51, 52 }, { 60, 61, 62 } },
                              { { 70, 71, 72 }, { 80, 81, 82 }, { 90, 91, 92 } } };

Since no size was specified the size was automatically determined for you. Alternately you can specify the size:

int[, ,] cube = new int[3,3,3] { { { 10, 11, 12 }, { 20, 21, 22 }, { 30, 31, 32 } },
                                 { { 40, 41, 42 }, { 50, 51, 52 }, { 60, 61, 62 } },
                                 { { 70, 71, 72 }, { 80, 81, 82 }, { 90, 91, 92 } } };

The first digit inside the brackets, 3, indicates  the number of groups of groups. The second digit, 2, indicates the number of groups of numbers, and the third digit indicates the count of numbers in the group.

You can now access any of the numbers using the following syntax.

    int val = cube[0,0,0];

The above will return the first item in the first pair, which is 10. Or to get the last number in the last group of number in the last group of groups do this:

    int val = cube[2,2,2];

However, because this is a multi-dimensional array, you cannot access the number groups or the groups of groups as arrays. The following two statements are NOT allowed.

    int[] singleD = cube[0,0];
    int[] twoD = cube[0,0];

That gives you a compile error. That is because the number pairs are not really stored in memory as C# array objects.

Jagged Array in C#

A jagged array is an array of variable-sized arrays.  For example, imagine you have the following arrays of numbers and you want an array of these arrays.

{ 10, 11, 12 }, { 20, 21, 22, 23, 24 }, { 30, 31 }, { 40, 41, 42, 43, 44, 45 }, { 50, 51, 52 }

This is often depicted as follows:

10 11 12
20 21 22 23 24
30 31
40 41 42 43 44 45
50 51 52

You would code this as follows:

    int[][] jagged = new int[][] { new int[]{ 10, 11, 12 }, new int[]{ 20, 21, 22, 23, 24 },
                                   new int[]{ 30, 31 }, new int[]{ 40, 41, 42, 43, 44, 45 },
                                   new int[]{ 50, 51, 52 } };

Notice that you have to actually instantiate each individual array inside the jagged array.

You can now access any of the numbers using the following syntax.

    int val = jagged[0][0];

The above will return the first item in the first pair, which is 10. Or to get the last number in the last group of number in the last group of groups do this:

    int val = jagged[4][2];

And because this is an array of arrays, you CAN access the internal groups as arrays. The following statement is VALID.

    int[] array1 = jagged[0];

Other tutorials

 

Leave a Reply

How to post code in comments?