yanghuan/CSharp.lua

Clone on 2-dimensional (2D) array returns a single dimensional array

joelverhagen opened this issue · 0 comments

Repro:

internal class Program
{
    private static void Main()
    {
        var grid = new int[5, 10];
        Console.WriteLine(grid.GetLength(0));
        Console.WriteLine(grid.GetLength(1));
        var clone = grid.Clone();
        var cast = (int[,])clone;
        Console.WriteLine(cast.GetLength(0));
        Console.WriteLine(cast.GetLength(1));
    }
}

Expected output:

5
10
5
10

Actual output:

C:\Users\jver\Downloads\lua-5.2.4_Win64_bin\lua52.exe: System.InvalidCastException: Unable to cast object of type 'System.Int32[]' to type 'System.Int32[,]'.
stack traceback:
        .\out.lua:19604: in function 'Main'
        .\out.lua:19621: in main chunk
        [C]: in function 'require'
        ...arp.lua\CSharp.lua.Launcher\bin\Debug\net7.0\DT\main.lua:1: in main chunk
        [C]: in ?

A workaround is a manual clone implementation (nested for loop). This is slower in .NET because it is not a block copy but I imagine it would be the same performance in Lua.