How to copy C # 3D gear array

I need to (initially) copy a C # 3D array without binding, foos , to another 3D array (and eventually add the dimensions x, y, z). I thought I could use the same syntax / logic to copy foos , as was used to create foos, as I did below (where row = 2, col = 3, z = 4):

private static void copyFooArray(Foo[][][] foos, ref Foo[][][] newArray)
{
    for (int row = 0; row < foos.Length; row++)
    {
        newArray[row] = new Foo[foos[row].Length][];

        for (int col = 0; col < foos[row].Length; col++)
        {
            newArray[row][col] = new Foo[foos[row][col].Length];

            for (int z= 0; z< foos[row][col].Length; z++)
            {
                newArray[row][col][z] = new Foo();
                newArray[row][col][z].member = foos[row][col][z].member;
            }
        }
    }            
        Console.Read();
}

but I get Index was outside the bounds of the array.in this line:

newArray[row] = new Foo[foos[row].Length][];

Why?

Foo Class:

public class Foo
{ 
    public string member;
}

Thanks.

+4
source share
1 answer

It doesn't seem like your reference array is initializing correctly. To set the value, your newArray must be initialized to the same size as your original.

- :

Foo[][][] firstFoo = new Foo[10][][];
Foo[][][] fooToCopy = new Foo[firstFoo.Length][][];

copyFooArray(firstFoo, ref fooToCopy);

, ref , # .

+3

Source: https://habr.com/ru/post/1606657/


All Articles