Kazuhiro Takahashi Kazuhiro Takahashi - 3 months ago 15
C# Question

Get an array of string from a jagged array

I have a jagged array that contains other 1d string arrays:

string[] first = {"one","two"};
string[] second = {"three","four"};
string[][] jagged = {first,second};


When I try to get the sub-arrays, they give a null value (I might be doing something wrong):

foreach (string[] arr in jagged[][]) {
// My stuff here
}


Did I do something wrong in the array initialization progress or do I have to convert the sub-arrays somehow?

Answer

Just the foreach part is wrong. I have tested it like as follows:

string[] first = { "one", "two" };
string[] second = {"three","four"};
string[][] jagged = {first,second};

foreach (string[] arr in jagged)
{
    Console.WriteLine(string.Join(",", arr));
}

Output:

one, two

three, four