保留列表的值 [英] retaining values of a list

查看:75
本文介绍了保留列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个列表,我将它分配给新列表,然后修改第一个列表,但第二个列表也会更新。任何避免更新第二个列表的方法。



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.

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也会使用samples2进行更新。如何避免这种情况?



samples1 also gets updated with samples2. How to avoid that?

推荐答案

这是因为在你的情况下,类 SampleData 是一个参考类型。复制此类型的实例时,实际上会获得引用同一对象的第二个引用。这样,您可以在两个不同的列表中引用同一组对象。如果您修改一个引用的对象属性(或任何其他实例成员)抛出其引用,另一个引用(在本例中,在第二个列表中)也引用相同的对象,一个修改过的对象。



如果您需要列表真正独立,请执行以下操作之一:
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. 生成 SampleData 键入 struct ,而不是 class
  2. 保持类,但 clone 元素这个类而不是复制它(也就是说,而不是只复制引用)。这个类非常简单,只需使用浅层克隆,这是通过方法 System.Object.MemberwiseClone 完成的:

    http://msdn.microsoft.com/en-us/library/system .object.memberwiseclone.aspx [ ^ ]。
  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[^].





参见:

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



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


这篇关于保留列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆