c# assign 1 dimensional array to 2 dimensional array syntax

前端 未结 4 1577
孤街浪徒
孤街浪徒 2021-02-07 10:10

I want to do something like:

object[] rowOfObjects = GetRow();//filled somewhere else
object[,] tableOfObjects = new object[10,10];

tableOfObjects[0] = rowOfObj         


        
4条回答
  •  抹茶落季
    2021-02-07 10:29

    No, if you are using a two dimensional array it's not possible. You have to copy each item.

    If you use a jagged array, it works just fine:

    // create array of arrays
    object[][] tableOfObject = new object[10][];
    // create arrays to put in the array of arrays
    for (int i = 0; i < 10; i++) tableOfObject[i] = new object[10];
    
    // get row as array
    object[] rowOfObject = GetRow();
    // put array in array of arrays
    tableOfObjects[0] = rowOfObjects;
    

    If you are getting all the data as rows, you of course don't need the loop that puts arrays in the array of arrays, as you would just replace them anyway.

提交回复
热议问题