C#C
C#3y ago
DiMiaN

✅ Outputting values from a list

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);
        }
    }
}
Was this page helpful?