4

Can someone explain the difference between these two declarations?

  1. double dArray[][];
  2. double dArray[,];
3
  • 1
    actually as posted this is not valid C# Commented Mar 1, 2011 at 2:49
  • because it's double[][] dArray; and double[,] dArray; Commented Mar 1, 2011 at 2:54
  • 1
    @user: I'm not @BrokenGlass, but unlike in C and Java, in C# you need to declare the type as double[][] and double[,] as opposed to placing the brackets on the variable name. Commented Mar 1, 2011 at 2:54

4 Answers 4

7
double dArray[][];

is Array-of-arrays while

double dArray[,];

is a two dimentional array.

it's easy enough to look them up.

MSDN Reference link

Sign up to request clarification or add additional context in comments.

Comments

3

The last syntax is easy, it declares a multidimensional array of doubles. Imagine the array is 3x2, then there would be 6 doubles in the array.

The 1st syntax declares a jagged array. The second syntax is rectangular or square, but this syntax need not be. You could have three rows, followed by 3 columns, then 2 columns, then 1 column, ie: its jagged.

2nd: 1-1, 1-2, 1-3
     2-1, 2-2, 2-3

1st: 1-1, 1-2, 1-3
     2-1, 2-2,
     3-1,

Comments

3

The first is an array of double arrays, meaning each separate element in dArray can contain a different number of doubles depending on the length of the array.

double[][] dArray = new double[3][];
dArray[0] = new double[3];
dArray[1] = new double[2];
dArray[2] = new double[4];

           Index
        0       1       2
      -----   -----   -----
L  1  |   |   |   |   |   |
e     -----   -----   -----
n  2  |   |   |   |   |   |
g     -----   -----   -----
t  3  |   |           |   |
h     -----           -----
   4                  |   |
                      -----

The second is called a multidimensional array, and can be thought of as a matrix, like rows and columns.

double[,] dArray = new dArray[3, 3];

         Column
        0   1   2
      -------------
   0  |   |   |   |
R     -------------
o  1  |   |   |   |
w     -------------
   2  |   |   |   |
      -------------

Comments

1

See the official documentation (click on the C# tab).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.