How to foreach through a 2 dimensional array?

Posted by Scott Langham on Stack Overflow See other posts from Stack Overflow or by Scott Langham
Published on 2010-05-14T12:51:04Z Indexed on 2010/05/14 12:54 UTC
Read the original article Hit count: 146

Filed under:

If I've got a 2 dimensional array.

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

And I'd like to foreach through it like this.

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

But, I get the error "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 1 dimensional array for that row?

Thanks.

© Stack Overflow or respective owner

Related posts about c#