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

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

问题描述

我有一个二维数组,

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?

推荐答案

如果您这样定义数组:

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

然后您可以在其上使用foreach循环.

Then you can use a foreach loop on it.

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

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