0

I have Static list

I add an amount to it, for example 50

Then I make a copy of this list

I am modifying the new list, for example 50 To 100

The question is why is he modifying the two lists .He is modifying the new list and original list

I want edit the new list just

    public class TestClass
    {
        public class Model
        {
            public long ID { get; set; }
            public decimal Amount { get; set; }
        }
        public static List<TestClass.Model> Staticlist { get; set; } = new List<TestClass.Model>();
    }

    static void Main(string[] args)
    {
        TestClass.Model model = new TestClass.Model();
        model.ID = 1;
        model.Amount = 50;
        TestClass.Staticlist.Add(model);

        var NewList = new List<TestClass.Model>(TestClass.Staticlist);

        foreach (var item in NewList)
        {
            if (item.ID == 1)
            {
                item.Amount = 100;
            }
        }

        var StaticlistAmount = TestClass.Staticlist.FirstOrDefault().Amount;
        var NewListAmount = NewList.FirstOrDefault().Amount;
        Console.WriteLine(StaticlistAmount);
        Console.WriteLine(NewListAmount);
        Console.ReadLine();
    }
forme forme
  • 43
  • 1
  • 6
  • Because you are playing with reference, if you will modify the value in one reference it will also change the value in other references as well. Please read deep copy vs shallow copy. [LINK](https://www.geeksforgeeks.org/shallow-copy-and-deep-copy-in-c-sharp/) – Pashyant Srivastava Jun 13 '21 at 09:45
  • When you create `NewList` you are only copying the references to the elements, but not creating copies of the elements. Ie the elements in `StaticList` and `NewList` point to the same object (read as same location in memory) – derpirscher Jun 13 '21 at 09:47
  • thank you for your reply . What is the best way to copy List with elements – forme forme Jun 13 '21 at 09:51
  • 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) – GSerg Jun 13 '21 at 09:56

3 Answers3

3

It is because your new List contains the same elements as the original List. You need to deep clone it to avoid this happening.

Peter Dongan
  • 1,762
  • 12
  • 17
0

you can use this code

public class TestClass
{
    public class Model
    {
        public long ID { get; set; }
        public decimal Amount { get; set; }
    }
    public static List<TestClass.Model> Staticlist { get; set; } = new List<TestClass.Model>();
}

rather copying reference, create new model and then play with it.

static void Main(string[] args)
{
TestClass.Model model = new TestClass.Model();
model.ID = 1;
model.Amount = 50;
TestClass.Staticlist.Add(model);

var result = from element in TestClass.Staticlist
             select new Model { ID = element.ID, Amount = element.Amount };


var NewList = result.ToList();

foreach (var item in NewList)
{
    if (item.ID == 1)
    {
        item.Amount = 100;
    }
}

var StaticlistAmount = TestClass.Staticlist.FirstOrDefault().Amount;
var NewListAmount = NewList.FirstOrDefault().Amount;
Console.WriteLine(StaticlistAmount);
Console.WriteLine(NewListAmount);
Console.ReadLine();
}
0

Your Model class is a reference type. When you assign an object to another variable, the actual object is not copied, it just copies the reference that points to the object:

var model = new TestClass.Model { ID = 10, Amount = 1000 };
var modelCopy = model;
modelCopy.Amount = 500;
Console.WriteLine(model.Amount) // Prints 500

Here, modelCopy is just another reference that points to the same object. Similarly, when you create a new list:

var NewList = new List<TestClass.Model>(TestClass.Staticlist);

This will create a new list, but all elements in both lists are references to the same objects.

A quick solution would be to first create a copy constructor:

    public class Model
    {
        public long ID { get; set; }
        public decimal Amount { get; set; }
        public Model() {}
        public Model(Model model) => (ID, Amount) = (model.ID, Amount = model.Amount); 
    }

Now you can copy the list using Linq:

var NewList = TestClass
    .Staticlist
    .Select(model => new TestClass.Model(model))
    .ToList();

Or if you don't want to modify the class:

var NewList = TestClass
    .Staticlist
    .Select(model => new TestClass.Model { ID = model.ID, Amount = model.Amount })
    .ToList();
Amal K
  • 4,359
  • 2
  • 22
  • 44