I have a simple model like this:
public class AssignStatisticsModel
{
public string TasksName { get; set; }
public int Time { get; set; }
}
I want to create four objects into it like:
List<AssignStatisticsModel> textModels = new List<AssignStatisticsModel>();
AssignStatisticsModel textmodel = new AssignStatisticsModel();
textmodel.TasksName = "Progress to Back Check";
textModels.Add(textmodel);
textmodel.TasksName = "Back Check to Corrections";
textModels.Add(textmodel);
textmodel.TasksName = "Corrections to Completed";
textModels.Add(textmodel);
textmodel.TasksName = "Progress to Completed";
textModels.Add(textmodel);
But for some reason all TasksNames called "Progress to Completed" instead create one with each TasksName I added. Why last textModels.Add(textmodel); replace four objects with same TasksName?
AssignStatisticsModelas if it were a "value type" (i.e., a struct) and not a "reference type" (i.e., the class that it is). You create a single AssignStatisticsModel, set some properties and then add the reference you have to that object to the list. Then you change the properties and re-add the same reference to the list again. But, there is only one object and so you get a list containing four references to the same object. If you had made your type a struct, it would have been copied 4 times each different