简体   繁体   中英

Add array to jagged array

I want to create a dynamic jagged array with dynamic data. The problem is that a part of the jagged array is only a two column type and the rest of it is a 4 column type. Code is in C#.

public static Object[][] my_array = new Object[20][];
public static void LoadData()
{
     for(int i = 0; i < 20; i++)
     {
         my_array[i]    = new Object[20];
         my_array[i][0] = "Data1";
         my_array[i][1] = "Data2";
         my_array[i][2] = "Data3";
         my_array[i][3] = "Data4";
         my_array[i][4] = new Object[100];

         for(int j = 0; j < 100; j++)
         {
             my_array[i][4][j] = new Object[200];
             my_array[i][4][j][0] = "SubData1";
         }
         my_array[i][5] = "Data6";
     }
 }

I get following error:

Severity Code Description Project File Line Suppression State Error CS0021 Cannot apply indexing with [] to an expression of type 'object'

Is this even possible to do this in C#?

您应在应用索引之前将其强制转换为Array,例如:

var array = (Object[]) my_array[i][4];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM