Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding items to a List in C#

Posted on 2016-08-23
6
Medium Priority
?
99 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 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 41

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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