Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a list and I am assigning it to new list and then modifying the first list but the second list also gets updated. Any way to avoid updating the second list.

C#
List<SampleData> samples = new List<SampleData>() 
{
new SampleData { Value1 ="1", Value2="1001", Value3 = null, Value4 = true},
new SampleData { Value1 ="2", Value2="1002", Value3 = "5002", Value4 = false},
new SampleData { Value1 ="3", Value2="1003", Value3 = "5003", Value4 = true}
};
  
List<SampleData> samples2 = new List<SampleData>() 
{
new SampleData { Value1 ="1", Value2="1001", Value3 = null, Value4 = true},
new SampleData { Value1 ="4", Value2="1002", Value3 = "5002", Value4 = false},
 new SampleData { Value1 ="5", Value2="1003", Value3 = "5003", Value4 = true}
 };

List<SampleData> samples1 = samples2;
foreach (var item in samples)
{
samples2.Where(s => s.Value1 != item.Value1).ToList().ForEach(t => { t.Value4 = true; });
}


public class SampleData
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public bool Value4 { get; set; }
}


samples1 also gets updated with samples2. How to avoid that?
Posted
Updated 24-Jul-13 4:22am
v2
Comments
ZurdoDev 24-Jul-13 10:20am    
Make a different copy, not a reference.
Sergey Alexandrovich Kryukov 24-Jul-13 10:38am    
It needs some explanation as the one who failed to understand the effect of the reference-type instance copy will hardly understand your comment. Please see my answer.
—SA
ZurdoDev 24-Jul-13 11:01am    
When I asked there was no code at all.
Sergey Alexandrovich Kryukov 24-Jul-13 11:59am    
No code, no problem, right? Anyway, I answered...
—SA
sanket.raj8 24-Jul-13 10:37am    
any extension method for copying?

1 solution

It happens because the class, in your case, SampleData, is a reference type. When you copy an instance of such type, you actually obtain a second reference referencing the same object. This way, you reference the same set of objects in two different lists. If you modify one referenced object property (or any other instance member) throw its reference, the other reference (in this case, in the second list), also referenced the same object, a modified one.

If you need the lists to be truly independent, do one of the following:
  1. Make SampleData type a struct, not class.
  2. Keep it a class, but clone elements of this class instead of copying it (that is, instead of copying just the reference). This class is so simple that you can simply use shallow cloning, which is done via the method System.Object.MemberwiseClone:
    http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^].


See also:
http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=vs.110%29.aspx[^],
http://en.wikipedia.org/wiki/Deep_copy#Shallow_copy[^],
http://en.wikipedia.org/wiki/Deep_copy#Deep_copy[^].

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900