Tareq
Tareq

Reputation: 473

Showing data from List with class in C#

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. :)

Upvotes: 1

Views: 960

Answers (2)

DAre G
DAre G

Reputation: 197

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();
    }

 }

Upvotes: 1

chenzhongpu
chenzhongpu

Reputation: 6871

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);

Upvotes: 3

Related Questions