1

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?

1
  • The answers below are correct. Your problem is that you are treating your AssignStatisticsModel as 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 Commented Aug 1, 2018 at 17:26

4 Answers 4

7

You are overwriting the TasksName property of the the same instance several times and add the same instance four times to your List. Collection initializer will fix your issue:

var textModels = new List<AssignStatisticsModel>()
{
    new AssignStatisticsModel {TasksName = "Progress to Back Check"},
    new AssignStatisticsModel {TasksName = "Back Check to Corrections"},
    new AssignStatisticsModel {TasksName = "Corrections to Completed"},
    new AssignStatisticsModel {TasksName = "Progress to Completed"},
};
Sign up to request clarification or add additional context in comments.

Comments

3

You have to create a new textmodel object each time. What you've done is added the same object 4 times to the list:

List<AssignStatisticsModel> textModels = new List<AssignStatisticsModel>();

var textmodel = new AssignStatisticsModel { TasksName = "Progress to Back Check" };
textModels.Add(textmodel);
textmodel = new AssignStatisticsModel { TasksName = "Back Check to Corrections" };
textModels.Add(textmodel);
textmodel = new AssignStatisticsModel { TasksName = "Corrections to Completed" };
textModels.Add(textmodel);
textmodel = new AssignStatisticsModel { TasksName = "Progress to Completed" };
textModels.Add(textmodel);

Comments

1

You are creating one instance (or object) of the AssignStatisticsModel class, and setting TasksName property four times on the same object. Passing an object to a method (Add method in your example) does not copy the object, instead it passes the reference to that exact object to the method. You are actually holding four references to one object, in your list. That's how reference types work. If AssignStatisticsModel was a value type instead (a struct instead of a class), then passing your variables to the method would copy their content.

Comments

0

To add to others answers here, you can research the difference between Reference and Value types. Here is a good link

Reference Type variables are stored in the heap while Value Type variables are stored in the stack. Value Type: A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. More Here

1 Comment

That's the worse possible explanation about value type and reference type. Sorry, but that difference (which is not accurate and exact at all) is of no value. That's an implementation detail, and not the behavioral difference that programmers need to know.

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.