Avatar of Adam Trask
Adam Trask
 asked on

Adding items to a List in C#

I am learning how to use Lists as part of C# generic collections. The code listed below is a simple class called Person with FirstName, LastName and Age as attributes for each employee in the Person Class.
After creating the class comes the List which is of type Person with an object called employees. Then I add two employees to the list.
All goes well except when I run the code I don't get the expected particulars of the second employee, instead I get "ConsoleApplication2.Person". I want to know what I am doing wrong.   Thanks.

namespace ConsoleApplication2
{
    class Person
    {

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }


        public Person(string FirstName, string LastName, int Age)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.Age = Age;
        }
   
        static void Main(string[] args)
        {
            List<Person> employees = new List<Person>();
            employees.Add(new Person("Tom", "Drake", 50));
           employees.Add(new Person("Kelly", "Drake", 40));
            Console.WriteLine(employees[1]);
            Console.ReadKey();



        }
    }
}
C#.NET Programming

Avatar of undefined
Last Comment
Adam Trask

8/22/2022 - Mon
SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
p_davis

you are printing the object not the strings that make up the object.
Adam Trask

ASKER
Thanks to both of you.  But I still don't know how to display the information on the console. Here is what I did:

class Person
    {

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }


        public Person(string FirstName, string LastName, int Age)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.Age = Age;
        }
   
        static void Main(string[] args)
        {
            List<Person> employees = new List<Person>();
            employees.Add(new Person("Tom", "Drake", 50));
           employees.Add(new Person("Kelly", "Drake", 40));
          //  Console.WriteLine(employees[1]);
          //  Console.ReadKey();

       }
        public override string ToString()
        {
            return "Person: " + FirstName + " " + LastName + " " + Age;
           
        }
    }
}
ASKER CERTIFIED SOLUTION
Kyle Abrahams

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Adam Trask

ASKER
Thank you Kyle
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Adam Trask

ASKER
Thanks.  Things are getting a bit more clearer