Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding items to a List in C#

Posted on 2016-08-23
6
Medium Priority
?
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 800 total points
ID: 41767152
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
ID: 41767156
you are printing the object not the strings that make up the object.
0
 

Author Comment

by:Adam Trask
ID: 41767196
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 1200 total points
ID: 41767255
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
ID: 41767350
Thank you Kyle
0
 

Author Closing Comment

by:Adam Trask
ID: 41767362
Thanks.  Things are getting a bit more clearer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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