mydarkpassenger
New member
- Joined
- Mar 26, 2017
- Messages
- 2
- Programming Experience
- 10+
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("-------------------------------");
Console.WriteLine(String.Format("{0,-10} | {1,-10}", "List 1", "List 2"));
Console.WriteLine("-------------------------------");
for (var i = 0; i < list1.Count; i++)
{
Console.WriteLine(String.Format("{0,-10} | {1,-10}", list1.ElementAt(i), list2.ElementAt(i)));
}
Console.WriteLine("-------------------------------");
//////////////////////////////
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?
///////////////////////////////
var list1 = new List<string>() { "Gene", "Rob", "Gary"};
var list2 = new List<string>() { "Mike", "Frank", "Sue" };
Console.WriteLine("-------------------------------");
Console.WriteLine(String.Format("{0,-10} | {1,-10}", "List 1", "List 2"));
Console.WriteLine("-------------------------------");
for (var i = 0; i < list1.Count; i++)
{
Console.WriteLine(String.Format("{0,-10} | {1,-10}", list1.ElementAt(i), list2.ElementAt(i)));
}
Console.WriteLine("-------------------------------");
//////////////////////////////
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?