我是否不小心绑定了两个可观察的集合?
在我的代码中,我有以下两个集合:
private ObservableCollection<Job> listOfJobs1 = new ObservableCollection<Job>();
private ObservableCollection<Job> listOfJobs2 = new ObservableCollection<Job>();
昨天,我尝试使用 listOfJobs1 中的对象填充 listOfJobs2,我这样做就像...
listOfJobs2 = listOfJobs1;
我注意到,我对 listOfJobs1 所做的任何更改都会反映在 listOfJobs2 中,即使在代码中也是如此。
这个“=”是否相当于以某种方式绑定集合以便它们互相观察?
我问的唯一原因是因为这个问题是通过在 listOfJobs1 上使用 foreach 并使用 Add() 以编程方式添加到 listOfJobs2 来解决的。如果我是对的,并且填充集合的两种方法不同,有人可以为我指出正确的方向,一篇解释其工作方式的文章,因为我现在要编写一个方法,该方法在很大程度上取决于这是否是这样的。另外,如果我使用 List<> 会出现这种情况吗?
In my code I have the two following collections:
private ObservableCollection<Job> listOfJobs1 = new ObservableCollection<Job>();
private ObservableCollection<Job> listOfJobs2 = new ObservableCollection<Job>();
Yesterday I attempted to populate listOfJobs2 with the objects from listOfJobs1, I did it like...
listOfJobs2 = listOfJobs1;
I noticed though that any changes I made to listOfJobs1 were then reflected in listOfJobs2, even in code well down the line.
Is this '=' the equivalent to somehow binding the collections so that they observe each other?
Only reason I ask is because this problem was solved by using a foreach on the listOfJobs1 and programmatically adding to listOfJobs2 using Add(). If I'm right and the two ways of populating a collection are different, can someone point me in the right direction to an article explaining the way this works because I'm about to write a method now that'll hugely depend on whether this is the case. Also, would this be the case if I were to use List<>?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
listOfJobs1 和listOfJobs2 都是对对象的引用(即它们指向对象的存储位置)。当您编写:
您是说使 listOfJobs2 中存储的引用等于 listOfJobs1 中存储的引用。换句话说,两个引用都将指向 listOfObjects1 最初引用的对象。此时,您可以将所有 listOfJobs2 替换为 listOfJobs1,并且应用程序的行为将相同。
可观察集合都包含对其所包含内容的引用列表,因此通过在循环中使用 Add() 将第一个列表中的所有引用添加到第二个列表中,您会说“嘿,列表 - 保留这些内容的记录” me”,而将列表 1 分配给列表 2,就像您所做的那样,是在说“嘿,对列表 2 的引用现在包含列表 1”,因此此时您只有列表 1 存储的对象列表。您已经完全丢失了对第二个列表对象的任何引用,并且您不再能够访问它。
listOfJobs1 and listOfJobs2 are both references to an object (ie they point to where the object is stored). When you write:
you are saying make the reference stored in listOfJobs2 equal the reference stored in listOfJobs1. In other words both references will point to the object originally refered to by listOfObjects1. At that point you could replace all listOfJobs2 with listOfJobs1 and the application would behave identically.
The observable collections both contain a list of references to the things they contain so by adding all the references from the first list to the second list using Add() in a loop you are saying "hey, list - keep a record of these things for me", whereas assigning list 1 to list 2, like you were doing, is saying "hey, the reference to list 2 now contains list 1" so you only have the list of objects stored by list one at that point. You've completely lost any reference to the object that was the second list object and you no longer have any access to it.