9

i used

double [,] marks=new double[26,5] 
int[] function = object.verify(marks) 

public void verifymarks(double[][] marks)

error i get is cannot convert from double[,] to double[][] i tried to search in the internet but couldnot find any solution. I have just began to use c#. Please Help. THankx in advance

user1700961
  • 129
  • 2
  • 8

3 Answers3

12

double[][] is jagged. It's literally an array of arrays.

double[,] is multidimensional. It gets special compiler treatment.

They have different memory layouts. double[][] is an array which holds references to the other arrays, while double[,] is laid out single-dimensionally for easier use.

Another difference is what data they can hold. A jagged array can be like this:

1231243245345345345345
23423423423423
2342342343r234234234234234234
23423

While a multidimensional array has to be a perfect table, like this:

34534534534534
34534534534533
34534534534534
34534534534545

A way to get around it would be to use nullable ints.

Now, to solve your problem:

Change your method signature to public void verifymarks(double[,] marks) and in the method change anything that uses marks[x][y] to marks[x,y].

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
4

double[][] is called a Jagged array. It is an array of array (the same way you could create a list of list). With this structure you can specify a different size for each sub-array.

For exemple:

double[][] jaggedArray = new double[2][];
jaggedArray[0] = new double[5];
jaggedArray[1] = new double[151];

The other writing double[,] is a 2D array (Multidimensional array in general). You can see it as a table.

A very handy feature of multidimensional array is the initialization:

string[,] test = new string[3, 2] { { "one", "two" }, 
                                    { "three", "four" },
                                    { "five", "six" } };
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • if both of them are array why can't i use jaggedarray[5][1]=multidimarray[5,1] – user1700961 Jul 22 '13 at 19:41
  • if `jaggedarray` and `multidimarray` contains the same data type it should work. – Cédric Bignon Jul 22 '13 at 19:43
  • i tried double[][] jaggedarray=new double[27][]; for (int j = 0; j < 20; j++) { jaggedarray[j][0] = values[j, 0]; jaggedarray[j][1] = values[j, 3]; } _but it did not work_ both of them are double – user1700961 Jul 22 '13 at 19:45
  • 3
    Because `jaggedarray[j]` is not initialized. You have to do `jaggedarray[j] = new double[2]` for each `j`. Before accessing its elements. – Cédric Bignon Jul 22 '13 at 19:46
2

Here's a visual, from LINQPad's Dump() function.

enter image description here

The first is the double[,], which creates a matrix. The second is double[][], which is just an array of arrays.

Bobson
  • 13,498
  • 5
  • 55
  • 80