✅ Outputting values from a list

DDiMiaN3/11/2023
When typing in name and age it doesnt display the correct values. Instead it outputs: Program+Person.
internal class Program
{
    public class Person
    {
        public string name { get; }
        public int age { get; set; }
        public int weight { get; set; }
        public int height { get; set; }

        public Person(string name, int age)
        {
            this.age = age;
            this.name = name;
            this.weight = 0;
            this.height = 0;
        }

        // Rest of the Person
    }

    private static void Main(string[] args)
    {
        List<Person> persons = new List<Person>();

        // Read the names of persons from the user
        while (true)
        {
            Console.Write("Enter a name, empty will stop: ");
            String name = Console.ReadLine();
            if (name == "")
            {
                break;
            }

            Console.Write("Enter the age of the person " + name + ": ");

            int age = Convert.ToInt32(Console.ReadLine());

            // Add to the list a new person
            // whose name is the previous user input
            persons.Add(new Person(name, age));
        }

        // Print the number of the entered persons, and their individual information
        Console.WriteLine();
        Console.WriteLine("Persons in total: " + persons.Count);
        Console.WriteLine("Persons: ");

        foreach (Person person in persons)
        {
            Console.WriteLine(person);
        }
    }
}
TTheRanger3/11/2023
please post code with $code instead
MMODiX3/11/2023
To post C# code type the following:
```cs
// code here
```

Get an example by typing $codegif in chat

If your code is too long, post it to: https://paste.mod.gg/
TTheRanger3/11/2023
$codegif
PPobiega3/11/2023
Console.WriteLine(person);
PPobiega3/11/2023
this will call ToString on person
PPobiega3/11/2023
and the default tostring for an object is to print the class name
PPobiega3/11/2023
you need to override ToString in your Person class
DDiMiaN3/11/2023
oh okay? I was following a simple tutorial and this is one of the examples
PPobiega3/11/2023
well either you do something like...
Console.WriteLine($"{person.Name}: {person.Age}");
PPobiega3/11/2023
or you override the ToString method in your class
DDiMiaN3/11/2023
thanks!
AAccord3/12/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
DDiMiaN3/12/2023
/close