1

Here i am using this code for copy one list to another

public class Person 
{
   public string Name { get; set; }
}

private void Form1_Load(object sender, EventArgs e)
{
    var originalList = new List<Person>();
    originalList.Add(new Person { Name = "name 1" });
    originalList.Add(new Person { Name = "name 2" });

    // var newList = originalList.ToList();
    var newList = new List<Person>(originalList);
    newList[0].Name = "New name";

    Console.WriteLine(originalList[0].Name);
}

My result in console is 'New name', why this is happen? When i am updating my new list it also updates my original one. How can i fix this?

DmO
  • 357
  • 2
  • 14
  • You've only done a clone of the list, not each of the items in it. The new list has the same items as the original. You're modifying the instance that exists at `[0]` in both lists. You need to do a deep copy. – madreflection May 05 '20 at 18:28
  • Your List contains references of objects. If you create a new List you just copy the references to these objects. If you change one of the objects, the reference does not change. – Sebastian Siemens May 05 '20 at 18:29
  • https://stackoverflow.com/a/222623/4394435 – Welcor May 05 '20 at 18:31
  • 1
    Does this answer your question? [How do I clone a generic list in C#?](https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c) – Pavel Anikhouski May 05 '20 at 18:31
  • Sorry i didn't understand, my new list is in the same memory location as the previous one? Or only it's items? – DmO May 05 '20 at 18:32
  • what you did only works for value types (int, ...) or strings. it does not work for a List of objects. you would copy the references to the new list, but the references still point to the same objects as the original list – Welcor May 05 '20 at 18:33
  • There's a separate memory location for the new list (container) but it just has references to the same memory locations for each of the items in it. – madreflection May 05 '20 at 18:35
  • Thank you madreflection. I also tried var newList = originalList.GetRange(0, originalList.Count); but i still have the same problem – DmO May 05 '20 at 18:35
  • 2
    That's no different. Think of it this way: Your boss puts you on a second team at work. That doesn't create a copy of you. Nothing you've tried so far creates copies of the objects, only the references to those objects. – madreflection May 05 '20 at 18:37
  • Nice explanation! :-) – DmO May 05 '20 at 18:38
  • So i need to create a new list, and adding each item manually, one by one. Is there any method easier from what i describe? – DmO May 05 '20 at 18:39
  • No, you dont add each item one by one. you create new items and copy the content of the original items into them. click on the links in the earlier comments. – Welcor May 05 '20 at 18:41
  • I found my answer var newList = originalList.Select(book => new Person()).ToList(); I just need to read more for understanding how this is working with references and objects – DmO May 05 '20 at 18:43
  • Good reading http://www.albahari.com/valuevsreftypes.aspx – Steve May 05 '20 at 18:47

1 Answers1

0

Do not worry, this is normal behavior, you have the original list, then you have another list filled in by the original, in your case, both lists point to the same items, which means that you have two references that point to the same memeory case , reason why you change an element from the original one the same element change in the second one and vice-versa .

Case 1 :

public class Person 
{
   public string Name { get; set; }
}

private void Form1_Load(object sender, EventArgs e)
{

   //here you have the original list, you create your list
   //you add element to your list .
   var originalList = new List<Person>();
   originalList.Add(new Person { Name = "name 1" });
   originalList.Add(new Person { Name = "name 2" });

   // you create a second list , but here the contain the
   // same element  than the original list
   var newList = new List<Person>(originalList);
   newList[0].Name = "New name";

   Console.WriteLine(originalList[0].Name);
}
sayah imad
  • 1,507
  • 3
  • 16
  • 24