Kazuhiro Takahashi Kazuhiro Takahashi - 1 year ago 72
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 Source

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));


one, two

three, four

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download