multidimensional array of object c#

16,065

Solution 1

You have couple of mistakes, in your code object[,] of =new object[lenght2][];

[,] is not equal to [][]

you can try this:

object[][] of = new object[length2][];
of[i] = Act; //it means you can assign `new[] { new object() };`

Read this: Multidimensional Array [][] vs [,]

it says that [,] is multidimensional array and [][] is array of arrays. So for your use array of arrays is valid.

Solution 2

In C# there are jagged arrays and multidimensional arrays. In your example you seem to be mixing up the two.

Jagged arrays are created this way, and you'll have to construct each "sub-array" individually:

object[][] obj = new object[10][];
obj[0] = new object[10];
obj[1] = new object[10];
...

Multidimensional arrays on the other hand:

object[,] obj = new object[10,10];

Solution 3

Multidimensional arrays (as opposed to jagged arrays) are always "rectangular", meaning that the length of each entry in the array is also fixed.

If you want just a list of arrays, which can have different lengths, then use List<object[]> as in

List<object[]> l = new List<object[]>();
l.Add(calcul_resporderbyact(responsable,v));

Or you could use a jagged array:

object[][] l = new object[length2][];
l[i] = calcul_resporderbyact(responsable,v);
Share:
16,065
Nancy
Author by

Nancy

Updated on June 04, 2022

Comments

  • Nancy
    Nancy almost 2 years

    I use an array of array :

    object[][] of =new object[lenght2][];
    

    Now, what i want is to insert a new array into of[][], I try this :

     for (int i = 0; i < lenght2; i++)
      {
          Act = calcul_resporderbyact(responsable,v); // return array of object
          of[i] = Act;
     }
    

    i want to know how to use some array from this multidimensional-array ??