Take the 2-minute tour ×

I confuse between double[][] and double[,] in C#.
My teammate give me a function like this:

public double[][] Do_Something(double[][] A)

I want to use this function:

double[,] data = survey.GetSurveyData(); //Get data
double[,] inrma = Do_Something(data);

It lead an error: invalid argument.
I don't want to edit my teammate's code.
Does it have any way to convert double[][] to double [,] ?


share|improve this question
double[][] is a jagged array. double[,] is a multidimensional array. They are definitely not the same type, and their uses differ rather a lot. Use the right type for the right task. –  J. Steen Jul 24 '13 at 12:49
Are you or your teammates sure about what is the difference between them? –  Soner Gönül Jul 24 '13 at 12:52
add comment

marked as duplicate by Cédric Bignon, poke, Jakub Konecki, Soner Gönül, bluefeet Jul 24 '13 at 14:14


4 Answers

up vote 6 down vote accepted

double[][] and double[,] have different meanings.

double[][] is jagged, so some elements can be of different lengths than others.

double[,] is "rectangular", so all elements are of the same length.

You could write a method to "convert" between the two, but how will you rectify the differences? I.e. how will you decide to "trim" from the long elements or expand the short elements in order to make it rectangular?

share|improve this answer
add comment

A double[][] is an array of double[] (An array of arrays) but double[,] is a single 2 dimensional double array

Example :

double[] Array1 = new double[] {1,2,3};
double[] Array2 = new double[] {4,5,6};
double[][] ArrayOfArrays = new double[][] {Array1,Array2};
double[,] MultidimensionalArray = new  double[,] {{1,2}, {3,4}, {5,6}, {7,8}};   
share|improve this answer
add comment
static T[,] Convert<T>(T[][] array)
    if (array == null)
        throw new ArgumentNullException("array");
    if (array.Length == 0)
        return new T[0, 0];
    T[,] retval = new T[array.Length, array[0].Length];
        for (int j = 0; j < array[i].Length; j++)
            if (array[i].Length != retval.GetLength(1))
                throw new Exception();
                retval[i, j] = array[i][j];
    return retval;
share|improve this answer
That only works with a non-jagged jagged array, to work with all inputs, you can use width = array.Max(a => a.Length), and fill the empty slots with default(T). If you do want to have the check in there, you should move it out a level, so it's only checked once per row, rather than for every cell. –  RoadieRich Jul 24 '13 at 13:05
add comment

They are two different types of arrays.

double[][] is a jagged array, which is an array of arrays; each of these arrays can have a different length, which leads to a problem.

double[,] is just a multidimensional array. Each row will have an equal number of columns and each column will have an equal number of rows.

This size difference causes a problem since the jagged array could really be different dimensions for different rows. You could write a method to convert between the two if you knew the exact dimensions of the jagged array, but in that case I would suggest rewriting the original method to accept and return a multidimensional array (double[,]).

share|improve this answer
add comment
