Is there any easy way to display 2 lists side by side in a console


New member
Mar 26, 2017
Programming Experience
I am working on a console program and I'd like to display 2 lists side by side. I was curious if I this is possible without needing to loop through them at the same time appending the items to the same Console.Write statement? The lists have different amount of elements and this doesn't seem practical. For example, here's 2 lists which will print out contents of 2 lists side by side:


var list1 = new List<string>() { "Gene", "Rob", "Gary"};
var list2 = new List<string>() { "Mike", "Frank", "Sue" };

Console.WriteLine(String.Format("{0,-10} | {1,-10}", "List 1", "List 2"));
for (var i = 0; i < list1.Count; i++)
Console.WriteLine(String.Format("{0,-10} | {1,-10}", list1.ElementAt(i), list2.ElementAt(i)));


This statement is not efficient for multiple reasons, least of which is it relies on 2 equal length lists (my data is not) I would prefer to loop through each list individually and write then write to the console specifying where the data should be on the screen if possible. I have workarounds, but I'm curious if there's a better way?
It's very easy to adapt your code to different sized lists.
var count1 = list1.Count;
var count2 = list2.Count;

for (var i = 0; i < Math.Max(count1, count2); i++)
    Console.WriteLine(String.Format("{0,-10} | {1,-10}",
                                    i < count1 ? list1[i] : string.Empty,
                                    i < count2 ? list2[i] : string.Empty));

Even if you don't do that, you should at least change your use of ElementAt to use the indexer as I have above. ElementAt is something you use for objects that implement IEnumerable but not IList specifically because such objects have no indexer.
Top Bottom