I am newbie to C#. If I created a collection with a number of objects.
Let us say my collection looks like this:
var theGalaxies = new List<Galaxy>
{
new Galaxy() { Name="Tadpole", MegaLightYears=400, Level=1},
new Galaxy() { Name="Pinwheel", MegaLightYears=25, Level=1},
new Galaxy() { Name="Milky Way", MegaLightYears=0, Level=2},
new Galaxy() { Name="Andromeda", MegaLightYears=3, Level=3}
};
foreach (Galaxy theGalaxy in theGalaxies)
{
Console.WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears);
}
How can I count how many objects is categorized in respective "level" and print out the information like the following output:
Name MegaLightYears Level
Tadpole 400 1
Pinwheel 25 1
2 galaxies in Level 1
Milky Way 0 2
1 galaxy in Level 2
Andromeda 3 3
1 galaxy in Level 3
Is there any way to print out information so it reserves a certain space for the input so the output does not look like this:
Name MegaLightYears Level
Tadpole 400 1
Pinwheel 25 1
Thanks for advance