Creating an array of two-dimensional arrays in C#

There's a simple answer to this. Use instances of the "System.Drawing.Point" class. Create a "Point" array to store coordinates. To create the array:

Point[] points = new Point[4];

And to assign a value to the array (e.g. at position 0) use the following code.

points[0] = new Point(xvalue,yvalue);//Where "xvalue" and "yvalue" are integer variables.

And to get the X and Y values from an instance of the point class. Use the below code.

int xvalue = points[0].X;

int yvalue = points[0].Y;

P.S. You can use these points to assign locations to Controls, but that's another story ^_^


One more curly bracket set {} is required in the initial declaration:

var waypoints = new int[4][,]   {
    new int[,] {{6}, {0}},
    new int[,] {{1}, {1}},
    new int[,] {{1}, {5}},
    new int[,] {{6}, {5}}
};

This is because for such to 2D array, each element in the array is considered as an array for the initialization (albeit it is typically used per element of the array such as val[0,0] = 4;).

Edit (after feedback from comments):

Put in contrast with int[][] (known as jagged array, that is: array of arrays whose array member can be of different size), int[,] is a 2D array with fixed dimension. Both are array which stores arrays, and therefore each element of the array is an array. This explains why there is a need to put another curly bracket in its initialization as above.

Such 2D array, when initialized differently, will result in different dimension (and thus there are multiple ways to initialize it):

int[,] val = new int[,] { { 6 }, { 0 } }; //resulting in int[2,1]
int[,] val = new int[,] { { 6, 0 } }; //resulting in int[1,2]

In either way, additional set of curly bracket is needed.

For the differences between jagged array and multidimensional, fixed sized, array, there are already plenty good explanations/benchmarking available online from well reputed sources. And I understand that it wouldn't be significant, apart from the OP's interest, for me to put more info about it. (And thus the scope for this answer is originally directed only to answer the failed initialization).

It is understood that the solution is not best used for storing coordinate points (as done by OP). The explanation above is given to explain why his initialization doesn't work, rather than to provide best solution for storing coordinate points. As for storing coordinate points, Point in the System.Drawing struct will be more proper (as suggested in the comment).

The use of 2D array to represent single point in 2D Cartesian coordinate is an "overkill", as a 1D array is already capable of storing as many numbers as the computer allows, much more than two numbers required to store points in Cartesian coordinate system.


The arrays are two-dimensional and have unknown size, the initializer can define arrays of various sizes:

        var waypoints = new int[4][,]   {
                new int[,] { {1, 2, 6}, {3, 4, 5} }, // two rows, 3 columns
                new int[,] { {1, 1}, {1, 2} } , // 2 x 2
                new int[,] { {1, 5} }, // 1 x 2
                new int[,] { {6, 5} }
                };