c# assign 1 dimensional array to 2 dimensional array syntax

前端 未结 4 1638
孤街浪徒
孤街浪徒 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:24

    If your array is an array of value types, it is possible.

    int[,] twoD = new int[2, 2] {
        {0,1},
        {2,3}
    };
    int[] oneD = new int[2] 
        { 4, 5 };
    int destRow = 1;
    Buffer.BlockCopy(
        oneD, // src
        0, // srcOffset
        twoD, // dst
        destRow * twoD.GetLength(1) * sizeof(int), // dstOffset
        oneD.Length * sizeof(int)); // count
    // twoD now equals
    // {0,1},
    // {4,5}
    

    It is not possible with an array of objects.

    Note: Since .net3.5 this will only work with an array of primitives.

提交回复
热议问题