-2
List<AnalyticResult> listanalytic = new List<AnalyticResult>();

foreach(AnalyticResult ar in listanalytic){
AnalyticResult artemp = new AnalyticResult();
artemp = ar;
artemp.name = name;
}

When I'm updating artemp.name it reflects in ar.name too. Update need to done only in artemp

Progman
  • 16,827
  • 6
  • 33
  • 48
  • 1
    Does this answer your question? [Why the other list also changes if I have changed the data of one list?](https://stackoverflow.com/questions/39362251/why-the-other-list-also-changes-if-i-have-changed-the-data-of-one-list) – Progman Nov 21 '22 at 21:08
  • Also check other questions like https://stackoverflow.com/questions/3040170/passing-objects-and-a-list-of-objects-by-reference-in-c-sharp, https://stackoverflow.com/questions/41816464/object-properties-changing-in-c-sharp-when-saving-an-old-copy or https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net – Progman Nov 21 '22 at 21:10

1 Answers1

0

Your problem is that you are not assigning the value of ar to artemp, but a reference to the ar object itself.

To break it down heavily: You are basically telling artemp at which point it can find the data, in this case beeing ar. You are not copying the data - you just creating a reference to the existing object ar. This means any mutation is made at the reference point itself - in this case ar.

To solve this issue you need to clone the object instead of just assinging it.

Kykos
  • 1
  • 2