1

I am trying to show data from list that has user defined type data.

Here is my class

namespace test
{
class Employee
{
    private string employeeName;
    private string employeeId;

    public override string ToString()
    {
        return "Employee Name: " + EmployeeName + "\nEmployee ID: " + EmployeeId;
    }

    public string EmployeeName
    {
        get { return employeeName; }
        set { employeeName = value;}
    }

    public string EmployeeId
    {
        get { return employeeId; }
        set { employeeId = value;}
    }
}
}

And here is my Main method,

namespace test
{
class Program
{
    static void Main(string[] args)
    {
        Employee employees = new Employee();
        List<Employee> listEmployee = new List<Employee>();

        employees.EmployeeName = "TEST 1";
        employees.EmployeeId = "01";

        listEmployee.Add(employees);

        employees.EmployeeName = "TEST 2";
        employees.EmployeeId = "02";

        listEmployee.Add(employees);

        Console.WriteLine("Number of Employee in the list: "+ listEmployee.Count);
        Console.WriteLine();

        foreach (Employee em in listEmployee)
        {
            Console.WriteLine(em);
            Console.WriteLine();
        }

     }
}
}

The output supposed to show,

Number of Employee in the list: 2

Employee Name: TEST 1
Employee ID: 01

Employee Name: TEST 2
Employee ID: 02

But it shows,

Number of Employee in the list: 2

Employee Name: TEST 2
Employee ID: 02

Employee Name: TEST 2
Employee ID: 02

Please tell me why I am not getting the expected output and how to overcome that.

Thank you in advance. :)

1
  • 1
    Chen greatly answered your question, but I would also suggest (for later code review) to rename your "employees" variable to "employee" and "listemployee" to "listOfEmployees" or just "employees" Commented Mar 6, 2016 at 5:22

2 Answers 2

3

You may have misunderstandings about object and reference.

You add an object, i.e. employees into the list:

enter image description here

Here the first element of the list holds the reference to the object. Hence when you make changes on the object, it becomes

enter image description here

Then you insert the object into the list again, and therefore the second position of the list also holds the reference to employees.

enter image description here

You should create a new object (with a new reference) before adding it to list -

Employee employees2 = new Employee();
employees2.EmployeeName = "TEST 2";
employees2.EmployeeId = "02";

listEmployee.Add(employees2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Chen. Now I am clear. Thank you again for spending time to draw all those graphics for just to make me clear. You are Awesome!!!! :-)
1

Your Modified main

static void Main(string[] args)
{
    Employee employeeFirst = new Employee();
    List<Employee> listEmployee = new List<Employee>();

    employeeFirst.EmployeeName = "TEST 1";
    employeeFirst.EmployeeId = "01";

    listEmployee.Add(employeeFirst);

    Employee employeeNext = new Employee();


    employeeNext.EmployeeName = "TEST 2";
    employeeNext.EmployeeId = "02";

    listEmployee.Add(employeeNext);

    Console.WriteLine("Number of Employee in the list: "+ listEmployee.Count);
    Console.WriteLine();

    foreach (Employee em in listEmployee)
    {
        Console.WriteLine(em);
        Console.WriteLine();
    }

 }

2 Comments

Darrell, Thank you a lot. :)
Aprreciated , Enjoy :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.