c# - Why does the object get duplicated multiple times? -


i want know understand way object creation/referencing happening. have piece of code employee objects in list duplicated. top code has issue overrides previous entries list contains duplicated objects whereas bottom fix. want understand why creating new object inside while.read() loop worked whereas having employee object creation outside of while loop did not.

public ienumerable<employee> employees {         {         list<employee> employees = new list<employee>();          using (sqlconnection connection = new sqlconnection(connectionstring))         {             employee _employee = new employee(); //<issue here, enties in employees duplicated values of single record             sqlcommand sqlcmd = new sqlcommand("spgetallemployees", connection);             sqlcmd.commandtype = system.data.commandtype.storedprocedure;             connection.open();             sqldatareader reader = sqlcmd.executereader();             while (reader.read())             {                 _employee.employeeid = convert.toint32(reader["employeeid"]); //object instantiation here fixed issue, why?                 _employee.name = (reader["name"].tostring());                 _employee.gender = (reader["gender"].tostring());                 _employee.city = (reader["city"].tostring());                 _employee.dateofbirth = convert.todatetime(reader["dateofbirth"]);                 _employee.departmentid = convert.toint32(reader["departmentid"]);                 employees.add(_employee);             }         }         return employees;     } } 

i want understand why fixed issue.

    public ienumerable<employee> employees             {                                 {                     list<employee> employees = new list<employee>();                      using (sqlconnection connection = new   sqlconnection(connectionstring))                 {                     sqlcommand sqlcmd = new sqlcommand("spgetallemployees", connection);                     sqlcmd.commandtype = system.data.commandtype.storedprocedure;                     connection.open();                     sqldatareader reader = sqlcmd.executereader();                     while (reader.read())                     {                         employee _employee = new employee(); //this fixed issue, why?                         _employee.employeeid = convert.toint32(reader["employeeid"]);                         _employee.name = (reader["name"].tostring());                         _employee.gender = (reader["gender"].tostring());                         _employee.city = (reader["city"].tostring());                         _employee.dateofbirth = convert.todatetime(reader["dateofbirth"]);                         _employee.departmentid = convert.toint32(reader["departmentid"]);                         employees.add(_employee);                     }                     }                     return employees;                 }             } 

in first example, have single _employee object, created outside loop. object stored in single reference in memory. when use add(_employee), not creating new object current properties of _employee, adding reference single object in memory list, multiple times. when change value of of properties of _employee on subsequent trips through loop, changing single object in memory, is, in effect, changing every item in list.

in second example, creating new _employee object every loop through while, , each new object has new memory address. therefore, every object add in list independent of last.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -