Solved

Adding items to a List in C#

Posted on 2016-08-23
6
84 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 200 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 300 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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