Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 84.6k

how to use loop in this oop code (bis)?

Aug 16 2022 8:10 AM

Hi,

A few days ago, i asked a question about how to use a loop in a oop code. the answer was to use  List<class>. This was ok because there was only one field. Now, suppose this code: with 2 different types of field. I use now 2 lists, one for each field. But then , i can't get the first player and his age on the first line, then the second etc... Now i get all the names on the first line, then all the ages. There must be a better way for this.

Thanks for hints.

V

class Tennis
{
    public string name;
     public int age;
 }

class Head
{
    static void Main(string[] args)
    {
        List<Tennis> name = new List<Tennis>();
        List<Tennis> age = new List<Tennis>();
       
        name.Add(new Tennis() { name = "Nadal" });
        age.Add(new Tennis() { age = 36 });
        
        naam.Add(new Tennis() { name = "Djokovic" });
        age.Add(new Tennis() { age = 35 });
       
        naam.Add(new Tennis() { name = "Federer" });
        age.Add(new Tennis() { age=41 });
       
        foreach (Tennis t in name)
            Console.Write(t.name + " ");
        Console.WriteLine();

        foreach (Tennis t in age)
            Console.Write(t.age + " ");
        Console.WriteLine();
    }
}


Answers (4)