繁体   English   中英

如何返回锯齿状数组

[英]How to return a jagged array

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

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

我尝试了类似的东西:

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

在我的主程序中:

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

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

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

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

在您的代码中:

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[][]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM