i need to build a list in a function, then return it to a another list variable the problem is: inside the function the list is built properly but when i return it to the outer variable it seems like all the elements of the outer list are all similar to the last one added to it inside the function. what is the reason of this?
this is my code:
List<stu> fillObject(SqlDataReader rd)
{
List<stu> stList = new List<stu>();
stu stObj = new stu();
while (rd.Read())
{
stObj.username = rd["username"].ToString();
stObj.password = rd["password"].ToString();
stObj.fname = rd["fname"].ToString();
stObj.lname = rd["lname"].ToString();
stObj.faculty = rd["faculty"].ToString();
stList.Add(stObj);
}
return stList;
}
here is the call of the function:
var friendsList = fillObject(rd);
so looping though 'friendList' would give the same last object added in the builder function.