Solved

Adding items to a List in C#

Posted on 2016-08-23
6
42 Views
Last Modified: 2016-08-23
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();



        }
    }
}
0
Comment
Question by:Adam Trask
6 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
Comment Utility
By default, Console.WriteLine will call the ToString method of an object that you pass to it. By default, objects--except for strings and ints (and such)--will print their type names. If you want to see the particulars, then you either have to dump out each property individually, or your have to override the default ToString implementation to dump out the values.
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
you are printing the object not the strings that make up the object.
0
 

Author Comment

by:Adam Trask
Comment Utility
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;
           
        }
    }
}
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 300 total points
Comment Utility
add this to your class:


public string Output()
{
 return "Person: " + FirstName + " " + LastName + " " + Age;
}

Open in new window


then in your code:

  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].Output());  //can also access any property, eg: employees[1].FirstName
           Console.ReadKey();
           

       }

Open in new window


Note that the main shouldn't be part of the person class.  You have a class which is responsible for itself.  Then you have a driver program which uses the class.
0
 

Author Comment

by:Adam Trask
Comment Utility
Thank you Kyle
0
 

Author Closing Comment

by:Adam Trask
Comment Utility
Thanks.  Things are getting a bit more clearer
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now