如何返回锯齿状数组

时间:2011-09-15 12:07:33

标签: c# return jagged-arrays

我有一个函数,它使用2D锯齿状数组来保存SQL查询中的记录。

如何正确返回锯齿状数组?

我尝试过类似的事情:

public string[][] GetResult()
{
    return result;
}

在我的主程序中:

string[][] test = new string[server1.GetResult().Length][];
test = server1.GetResult();

嗯,正如预期的那样,它没有用。

我不知道如何解决我的问题。

1 个答案:

答案 0 :(得分:1)

锯齿状数组只是数组的数组。

在您的代码中:

string[][] test = new string[server1.GetResult().Length][];
test = Gronforum.GetResult();

首先将新数组分配给test,然后使用GetResult()的返回值覆盖它。代码与:

相同
string[][] test = Gronforum.GetResult();

现在GetResult()应该返回一个string[][] - 试试这个以获得使用锯齿状数组的感觉:

public string[][] GetResult()
{
    string[][] result = new string[2][];
    result[0] = new string[] { "1", "2" };
    result[1] = new string[2];
    result[1][0] = "a";
    result[1][1] = "b";
    return result;
}

您可以向该方法提供对SQL操作结果的引用,以便它可以访问数据,将其“转换”为string[][]