© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
14 replies
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);
        }
    }
}
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);
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Show values from a list in a datagridview getting source from bindingsource
C#CC# / help
3y ago
✅ Tic-Tac-Toe validation outputting wrong values
C#CC# / help
3y ago
add multiple values inside a list
C#CC# / help
4y ago
❔ outputting data from database into a new form
C#CC# / help
3y ago