[Solved] C# Arrays in List printing to console in formatted way [closed]


Simplest solution if all arrays have same length:

string rowFormat = "{0,15}|{1,3} {2,3}";
Console.WriteLine(rowFormat, "History", "B", "W");
Console.WriteLine(new String('=', 25));

for(int i = 0; i < array1.Length; i++)    
    Console.WriteLine(rowFormat, array1[i], array2[i], array3[i]);

But I would suggest to use custom type for your data instead of keeping data in three arrays. E.g. (names according to your comments)

public class UserGuess
{
    public int AttemptNumber { get; set; }
    public int BlacksCount { get; set; }
    public int WhitesCount { get; set; }
}

But formatting will stay same:

foreach(var g in guesses)
    Console.WriteLine(rowFormat, 
       g.AttemptNumber, g.BlacksCount, f.WhitesCount);

NOTE: As The format item section on String.Format documentation states, format item has following syntax { index[,alignment][ :formatString] }. So, code above just uses alignment parameter to set fixed total length of field.

10

solved C# Arrays in List printing to console in formatted way [closed]