The arrays we used so far were made of a uniform series,
where all members consisted of a simple list, like a column of names on a
piece of paper. Also, all items fit in one list. This type of array is
referred to as one-dimensional.

In some cases, you may want to divide the list in
delimited sections. For example, if you create a list of names, you may want
part of the list to include family members and another part of the list to
include friends. Instead of creating a second list, you can add a second
dimension to the list. In other words, you would create a list of a list, or
one list inside of another list, although the list is still made of items
with common characteristics.

A multidimensional array is a series of arrays so that
each array contains its own sub-array(s).

Application:
Introducing Multidimensional Arrays

Launch your programming environment

To create a new application, on the main menu, click File -> New
Project...

Press Enter to close the DOS window and return to your programming
environment

Creating a Two-Dimensional Array

The most basic multidimensional array is made of two
dimensions. This is referred to as two-dimensional. To create a
two-dimensional array, declare the array variable as we have done so far but
add a comma in the square brackets. The formula you would use is:

DataType[,]VariableName;

The pair of brackets is empty but must contain a comma.
There are various ways you can initialize a two-dimensional array. If you
are declaring the array variable but are not ready to initialize it, use the
following formula:

DataType[,]VariableName= newDataType[Number1,Number2];

Or you can use the var keyword:

varVariableName= newDataType[Number1,Number2];

Either way, in the right pair of square brackets, enter
two integers separated by a comma. Here is an example:

In our declaration, the members variable
contains two lists. Each of the two lists contains 4 elements. This means
that the first list contains 4 elements and the second list contains 4
elements. Therefore, the whole list is made of 8 elements (2 * 4 = 8).
Because the variable is declared as a string, each of the 8 items
must be a string.

You can also create a two-dimensional
array that takes more than two lists, such as 3, 4, 5 or more. Here is an
example:

This time, the variable has 5 lists and
each list contains 8 elements. This means that the whole variable contains 5
* 8 = 40 elements.

If you want to declare the array variable
using the first formula where you don't know the sizes of the lists, you
must use the data type of the array and you can omit the right square
brackets. Here is an example:

In this case also, remember that you need the square
brackets. If you use the var keyword, you don't need the left square
brackets but, in the right square brackets, you must specify the sizes.

You can initialize an array variable when declaring it.
To do this, on the right side of the declaration, before the closing
semi-colon, type an opening and a closing curly brackets. Inside of the
brackets, include a pair of an opening and a closing curly brackets for each
internal list of the array. Then, inside of a pair of curly brackets,
provide a list of the values of the internal array, just as you would do for
a one-dimensional array. Here is an example:

When initializing a two-dimensional array, remember the
dimensions. The number on the left side of the right comma specifies the
number of main lists and the number on the right side of the right comma
specifies the number of elements in each list. Here is an example:

To use the members of a two-dimensional array, you can
access each item individually. For example, to initialize a two-dimensional
array, you can access each member of the array and assign it a value. The
external list is zero-based. In other words, the first list has an index of
0, the second list has an index of 1. Internally, each list is zero-based
and behaves exactly like a one-dimensional array. To access a member of the
list, type the name of the variable followed by its square brackets. In the
brackets, type the index of the list, a comma, and the internal index of the
member whose access you need. If you create an array without initializing
using the curly brackets, you can use this technique of accessing the
members by the square brackets to initialize each member of the array. Here
is an example:

using System;
public static class Exercise
{
static int Main(string[] args)
{
var members = new string[2,4];
members[0, 0] = "Celeste"; // Member of the First List
members[0, 1] = "Mathurin"; // Member of the First List
members[0, 2] = "Alex"; // Member of the First List
members[0, 3] = "Germain"; // Member of the First List
members[1, 0] = "Jeremy"; // Member of the Second List
members[1, 1] = "Mathew"; // Member of the Second List
members[1, 2] = "Anselme"; // Member of the Second List
members[1, 3] = "Frederique"; // Member of the Second List
return 0;
}
}

You can use this same technique to retrieve the value of
each member of the array. Here is an example:

using System;
public static class Exercise
{
static int Main(string[] args)
{
var members = new string[2,4];
members[0, 0] = "Celeste"; // Member of the First List
members[0, 1] = "Mathurin"; // Member of the First List
members[0, 2] = "Alex"; // Member of the First List
members[0, 3] = "Germain"; // Member of the First List
members[1, 0] = "Jeremy"; // Member of the Second List
members[1, 1] = "Mathew"; // Member of the Second List
members[1, 2] = "Anselme"; // Member of the Second List
members[1, 3] = "Frederique"; // Member of the Second List
Console.WriteLine(members[0, 0]);
Console.WriteLine(members[0, 1]);
Console.WriteLine(members[0, 2]);
Console.WriteLine(members[0, 3]);
Console.WriteLine(members[1, 0]);
Console.WriteLine(members[1, 1]);
Console.WriteLine(members[1, 2]);
Console.WriteLine(members[1, 3]);
return 0;
}
}

As we described it earlier, a two-dimensional array is a
list of two arrays, or two arrays of arrays. In other words, the second
arrays are nested in the first arrays. In the same way, if you want to
access them using a loop, you can nest one for loop inside of a first
for loop. The external loop accesses a member of the main array and
the second loop accesses the internal list of the current array. Here is an
example:

using System;
public static class Exercise
{
static int Main(string[] args)
{
var members = new string[2,4];
members[0, 0] = "Celeste"; // Member of the First List
members[0, 1] = "Mathurin"; // Member of the First List
members[0, 2] = "Alex"; // Member of the First List
members[0, 3] = "Germain"; // Member of the First List
members[1, 0] = "Jeremy"; // Member of the Second List
members[1, 1] = "Mathew"; // Member of the Second List
members[1, 2] = "Anselme"; // Member of the Second List
members[1, 3] = "Frederique"; // Member of the Second List
for (int External = 0; External < 2; External++)
for (int Internal = 0; Internal < 4; Internal++)
Console.WriteLine(members[External, Internal]);
return 0;
}
}

To apply a foreach operator, access only each
member of the internal list. Here is an example:

using System;
public static class Exercise
{
static int Main(string[] args)
{
var members = new string[2, 4];
members[0, 0] = "Celeste"; // Member of the First List
members[0, 1] = "Mathurin"; // Member of the First List
members[0, 2] = "Alex"; // Member of the First List
members[0, 3] = "Germain"; // Member of the First List
members[1, 0] = "Jeremy"; // Member of the Second List
members[1, 1] = "Mathew"; // Member of the Second List
members[1, 2] = "Anselme"; // Member of the Second List
members[1, 3] = "Frederique"; // Member of the Second List
foreach (var Internal in members)
Console.WriteLine(Internal);
return 0;
}
}

Application:
Accessing the Members

To retrieve the values of a two-dimensional array, change the file
as follows:

Press Enter to close the DOS window and return to your programming
environment

Creating and Using a Multidimensional Array

Introduction

Beyond two dimensions, you can create an array variable
that represents various lists, and each list contains various internal
lists, and each internal list contains its own elements. This is referred to
as a multidimensional array. One of the rules you must follow is that, as
always, all members of the array must be of the same type.

Creating a Multidimensional Array

To create a multidimensional array, add as many commas
in the square brackets as you judge them necessary. If you only want to
declare the variable without indicating the actual number of lists, you can
specify the data type followed by square brackets and the commas. Here is an
example that represents a three-dimensional array that is not initialized:

In this example, we are creating 2 groups of items. Each
of the two groups is made of three lists. Each list contains 5 numbers. As a
result, the array contains 2 * 3 * 5 = 30 members.

Initializing a Multidimensional Array

As always, there are various ways you can initialize an
array. To initialize a multidimensional array when creating it, you use an
opening and a closing curly brackets for each list but they must be nested.
The most external pair of curly brackets represents the main array. Inside
of the main curly brackets, the first nested curly brackets represent a list
of the first dimension. Inside of those brackets, you use another set curly
brackets to represent a list that would be nested. You continue this up to
the most internal array. Then, in that last set, you initialize the members
of the array by specifying their values. Here is an example:

To access a member of a multidimensional array, type the
name of the array followed by the opening square bracket, followed by the
0-based first dimension, followed by a comma. Continue with each dimension
and end with the closing square bracket. You can use this technique to
initialize the array if you had only created it. Here is an example:

Number = 12.44
Number = 525.38
Number = -6.28
Number = 2448.32
Number = 632.04
Number = -378.05
Number = 48.14
Number = 634.18
Number = 762.48
Number = 83.02
Number = 64.92
Number = -7.44
Number = 86.74
Number = -534.6
Number = 386.73
Number = 48.02
Number = 120.44
Number = 38.62
Number = 526.82
Number = 1704.62
Number = 56.85
Number = 105.48
Number = 363.31
Number = 172.62
Number = 128.48
Number = 906.68
Number = 47.12
Number = -166.07
Number = 4444.26
Number = 408.62
Press any key to continue . . .

Like an array of a primitive type, a multidimensional
array can be made a field of a class. You can primarily declare it without
initializing it. Here is an example:

public class TriangleInCoordinateSystem
{
private int[,] Points;
}

This indicates a two-dimensional array field: the array
will contain two lists but we don't know (yet) how many members each array
will contain. Therefore, if you want, when creating the array, you can
specify its dimension by assigning it a second pair of square brackets using
the new operator and the data type. Here is an example:

A multidimensional array can be passed as argument. When
creating the method, in its parentheses, enter the data type followed by the
square brackets. In the square brackets, enter one comma for a
two-dimensional array, two or more commas, as necessary, for a
three-dimensional arrays as necessary. Here is an example:

As indicated with one-dimensional arrays, when passing a
multi-dimensional array as argument, the array is treated as a reference.
This makes it possible for the method to modify the array and return it
changed. If you want to indicate that the array is passed by reference, you
can precede its name in the parentheses by the the ref keyword. Here
is an example:

You can return a multi-dimensional array from a method.
When creating the method, before its name, specify the data type followed by
square brackets. In the square brackets, enter the necessary number of
commas. Here is an example:

In the body of the method, you can do what you want but,
before exiting it, you must return an array of the same type that was
created. When calling the method, you can assign it to an array of the same
type it returns. Here is an example:

As done for primitive data types, you can create a
multi-dimensional array where each member is of a class type. Of course you
can use an existing class or you must first create a class. Here is an
example:

The ClassName factor is the name of the class
that will make up each member of the array. The other sections follow the
same rules we reviewed for the primitive types. For example, you can create
an array without allocating memory for it as follows:

If you know the number of members that
the array will contain, you can use the right pair of square brackets, in
which case you can specify the name of the class or use the var
keyword and omit the left square brackets. Here is an example:

This declaration creates a two-dimensional array of two
Point objects: The array contains two lists and each list contains two
Points.

To initialize a multidimensional array of objects, you
can access each array member using its index, allocate memory for it using
the new operator. After allocating memory for the member, you can
then access its fields or properties to initialize it. Here is an example:

You can also initialize the array when creating it.
Before doing this, you would need a constructor of the class and the
constructor must take the argument(s) that would be used to initialize each
member of the array. To actually initialize the array, you would need a pair
of external curly brackets for the main array. Inside of the external curly
brackets, create a pair of curly brackets for each sub-dimension of the
array. Inside the last curly brackets, use the new operator to access
an instance of the class and call its constructor to specify the values of
the instance of the class. Here is an example:

You can also use a loop to access the members of the
array. To do this, create a first for loop that stops at the first
dimension of the array - 1. Then, inside of a first for loop, nest a
for loop for each subsequent dimension. Here is an example:

As done for primitive types, a multidimensional array of
objects can be made a field of a class. You can declare the array without
specifying its size. Here is an example:

public class Triangle
{
public Point[,] Vertices;
}

If you know the dimensions that the array will have, you
can specify them using the new operator at the same time you are creating
the field. Here is an example:

public class Triangle
{
public Point[,] Vertices = new Point[3,2];
}

This creation signals a multidimensional array of
Point objects. It will consist of three lists and each list will contain
two Point objects

To initialize the array, access each member by its index
to allocate memory for it. Once you get the member, you access each one of
its fields or properties and initialize it with the desired value. Here is
an example:

You can pass a multidimensional array of objects as
arguments. To do this, in the parentheses of the method, enter the class
name followed by the square brackets. In the square brackets, type the
appropriate number of commas. Here is an example:

public class Triangle
{
public void Create(Point[,] Points)
{
}
}

In the body of the method, use the array as you we have
done so far. You can access its members to get to its values. Here are
examples:

Remember that an array passed as argument is in fact
passed by reference. You can indicate this by preceding it with the ref
keyword.

Returning a Multidimensional Array of Objects

A method can return a multidimensional array of objects.
If you are creating the method, before its name, type the name of the class
followed by square brackets. Inside the square brackets, type the desired
number of commas to indicate the dimension of the returned value. Here is an
example:

public class Triangle
{
public Point[,] Create()
{
}
}

After implementing the method, before exiting it, make
sure it returns the type of array that it was indicated to produce.
Here is an example: