如何通过二维数组“foreach"? [英] How do I 'foreach' through a two-dimensional array?

查看:26
本文介绍了如何通过二维数组“foreach"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组,

string[,] table = {
                       { "aa", "aaa" },
                       { "bb", "bbb" }
                   };

我想像这样foreach通过它,

foreach (string[] row in table)
{
    Console.WriteLine(row[0] + " " + row[1]);
}

但是,我收到错误:

无法将类型字符串转换为字符串[]

Can't convert type string to string[]

有没有办法实现我想要的,即迭代数组的第一维,迭代器变量返回该行的一维数组?

Is there a way I can achieve what I want, i.e. iterate through the first dimension of the array with the iterator variable returning me the one-dimensional array for that row?

推荐答案

如果你这样定义你的数组:

If you define your array like this:

string[][] table = new string[][] {
                       new string[] { "aa", "aaa" },
                       new string[]{ "bb", "bbb" }
};

然后你可以在上面使用 foreach 循环.

Then you can use a foreach loop on it.

这篇关于如何通过二维数组“foreach"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆