C# 如何扩展锯齿数组

C# 如何扩展锯齿数组,c#,arrays,list,multidimensional-array,jagged-arrays,C#,Arrays,List,Multidimensional Array,Jagged Arrays,我尝试用array.AddRange扩展锯齿数组,但没有成功。 我不知道为什么它不工作,我也不例外,但我的数组范围没有改变。 以下是我使用的代码: public World( ushort[][][] worldMatrice) { // OldWith = 864 ushort OldWidth = (ushort)worldMatrice.GetLength(0); // Extend the matrice to (1024) => only the fir

我尝试用
array.AddRange
扩展锯齿数组,但没有成功。 我不知道为什么它不工作,我也不例外,但我的数组范围没有改变。 以下是我使用的代码:

public World( ushort[][][] worldMatrice)
{
    // OldWith = 864
    ushort OldWidth = (ushort)worldMatrice.GetLength(0);

    // Extend the matrice to (1024) => only the first level [1024][][]            
    worldMatrice.ToList().Add(new ushort[1024- OldWidth][]);
    // NewWidth = 864 , and should be 1024 ...
    ushort NewWidth = worldMatrice.getLenght(0);
}
这个

将创建阵列的副本,然后您什么也没做

Array.AddRange()不会更改数组的维度,Array.Length将始终返回数组可以容纳的最大元素数,而不是数组中非空元素的总数

如果要更改数组的维度,可能需要将值从旧数组传输到具有所需维度的新数组

int[] newArray = new int[1024];
Array.Copy(oldArray, newArray, oldArray.Length);
要获取数组中非空元素的数量,请使用

int count = array.Count(s => s != null);

您没有保存输出。 试试这个:

    worldMatrice = worldMatrice.ToList().Add(new ushort[1024- OldWidth][]).ToArray();

Array.AddRange
,那不是javascript吗?在.NET中,数组的大小不可调整。您可以分配一个新数组并在元素上进行复制,但不能更改现有数组的大小。在任何情况下,您都没有告诉我们“不工作”是什么意思,也没有向我们展示尝试使用它的代码以及获得的结果。锯齿数组只是一维数组。您可以像调整任何其他一维数组一样调整其大小。请参阅标记的复制以获取指导。因此,我应该=>worldMatrice=worldMatrice.toList().Add(…?“Array.AddRange()不会更改数组的维度”--是的,它会更改。但是它是JavaScript,而不是C#。此外,应该告诉想要调整数组大小的人使用
Array.resize()
,而不是显式地自己编写逻辑。谢谢,这是一项伟大的工作
    worldMatrice = worldMatrice.ToList().Add(new ushort[1024- OldWidth][]).ToArray();