How do I print a list of string arrays? I can do it from string[] using Console.WriteLine, but if I do that for a list with foreach it just prints out System.String[]. How do I write an index when using a foreach?
5 Answers
So you have list of string arrays, like this:
List<string[]> data = new List<string[]>() {
new string[] {"A", "B", "C"},
new string[] {"1", "2"},
new string[] {"x", "yyyy", "zzz", "final"},
};
To print on, say, the Console, you can implement nested loops:
foreach (var array in data) {
Console.WriteLine();
foreach (var item in array) {
Console.Write(" ");
Console.Write(item);
}
}
Or Join the items into the single string and then print it:
using System.Linq;
...
string report = string.Join(Environment.NewLine, data
.Select(array => string.Join(" ", array)));
Console.Write(report);
Or combine both methods:
foreach (var array in data)
Console.WriteLine(string.Join(" ", array));
Console.WriteLineyou should either print each item of array separately or convert array to string and then print that string. E.g. withString.Join(",", yourArray)