将数组添加到锯齿状数组 c#

Adding array to Jagged Array c#

我都看过 ASP.Net c# adding items to jagged array and 但我似乎无法解决这个问题...

所以,基本上就是这样。我有一个函数可以根据一两个查询构建值列表。我将每个值 returned 添加到列表中,然后将列表转换为数组。一切都很好

不过,我现在需要return两个值,所以多维数组显得更合适。本质上,这就是我想要做的:

        string[][] array2D = new string[2][];
        array2D[0] = new string[3] { "one", "two", "three" };
        array2D[1] = new string[3] { "abc", "def", "ghi" };

到目前为止一切都很好。但是,我不知道我想在数组初始化时插入数组的值,所以,这就是我 期望 能够做到的:

        string[][] array2D = new string[2][];
        //array2D[0] = new string[3] { "one", "two", "three" };
        //array2D[1] = new string[3] { "abc", "def", "ghi" };

        string[] deviceIDS = { "one", "two", "three" };
        string[] groupIDS = { "abc", "def", "ghi" };

        array2D[0] = new string[deviceIDS.Length] deviceIDS;
        array2D[1] = new string[deviceIDS.Length] groupIDS;

但它真的不喜欢最后两行,报告它需要一个;

您已在此处创建数组:

string[] deviceIDS = { "one", "two", "three" };
string[] groupIDS = { "abc", "def", "ghi" };

所以你只需要设置对这些数组的引用:

array2D[0] = deviceIDS;
array2D[1] = groupIDS;