如何将父列表复制到子列表,以便在更改子列表中的值后,父列表值不会更改 [英] How copy a parent list to child list so that after changing value in child list parent list value does not changed

查看:71
本文介绍了如何将父列表复制到子列表,以便在更改子列表中的值后,父列表值不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个例子中,我创建了一个父类,我声明了一个属性ID。

我创建一个父类列表,设置值id = 10;将此列表复制到另一个列表后,在更改子列表的值后,它将自动更改父列表的值。



我想要的是不更改子列表的值子列表值更改后。



In this example i creat a parent class in whic i declare a properties ID.
I create a list of parent a set value id=10; After copy this list to another list and after changing value of child list it will automatically changed the value of parent list.

I want does not changed the value of child list after child list value changed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Parent> pList = new List<Parent>();
            Parent en = new Parent();
            en.ID = 10;
            pList.Add(en);


            List<Parent> cList = new List<Parent>();
            cList = new List<Parent>(pList);
            cList[0].ID = 11;
        }
    }

    public class Parent
    {
        public int ID { get; set; }
    }
}

推荐答案

如果您希望子列表不影响父列表,你必须复制内容:但由于列表的内容是引用的集合,只是复制列表不会这样做,因为你在新的列表中得到相同的引用!



List1:

If you want the "child" list not to affect the "parent" list, you have to copy the content: but since the content of a list is a collection of references, just copying the list doesn't do it, because you get the same references in a new list!

List1:
--> A
--> B

将List1复制到List2:

Copy List1 to List2:

--> A
--> B



现在,你可以通过添加项目来改变List1而不影响List2:


Now, you can alter List1 without affecting List2, by adding items:

--> A
--> B
--> C




--> A
--> B



但它引用的项目是相同的,所以如果你通过任何一个列表影响它,你会影响相同的项目。



让我们退后一步,想想现实世界。

你有两张纸。

你走进马路,列出你街上所有车辆的清单,以及他们停在外面的房子。

然后你回家,把清单复制到另一张清单上面。

你等一个小时,然后你到外面更新第一个清单以反映现在的汽车。



显然,这不会影响第二张纸,但是第一张纸上的一些车已经移动了位置,或者已经去了其他地方,或者新车已经到了。



但是如果你去了到了一个房子的地址,然后在那里看车,它是同一辆车,无论你通过哪个列表。因此,如果你砸掉15号以外的汽车的窗户,那么如果通过第一张或第二张,它仍会被粉碎。

列表不同,但车辆是相同的实例。 br />


这有意义吗?


But the items it refers to are the same, so if you affect it via either list, you affect the same item.

Let's take a step back, and think about the real world.
You have two sheets of paper.
You go into the road, and make a list of all the cars in your street, together with the house they are parked outside on one of them.
You then go home, and copy the list top the other sheet.
You wait an hour, then you go outside and update the first list to reflect the cars that are there now.

Obviously, this doesn't affect the second sheet of paper, but some of the cars on the first sheet have moved position, or have gone elsewhere, or new cars have arrived.

But if you go to the address of a house and look at the car there it is the same car, regardless of which list you go via. So if you smash the windows of a car outside number 15, then it is still smashed if go via the first sheet or the second.
The lists are different, but the cars are the same instances.

Does that make sense?


这篇关于如何将父列表复制到子列表,以便在更改子列表中的值后,父列表值不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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