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();
}