user3345626 user3345626 - 3 months ago 8
C# Question

Syntax to create a double[,] in C#

This seems extremely simple, yet I can't seem to find applicable documentation anywhere. In C#, how do you create a 'double[,]'? Specifically, the data I'm trying to represent is something like this:

[0,0] = 0
[0,1] = 1
[1,0] = 1
[1,1] = 2


I have tried [[0,1],[1,2]] and the equivalent with {{}{}} and {[][]} and various other things, but cannot seem to figure out the syntax. It seems that a simple [0,1] alone is a 'double[,]' but I would like to find a way to represent the above data (more than just 2 numbers).

What am I missing? If anyone can point me to some simple documentation, that would be great.

Answer

See Array initializers:

For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level.

In our case:

double[,] a = { { 0, 1 }, { 1, 2 } };