0

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<>?

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • It looks like the = operator is doing a shallow copy (see http://weblogs.asp.net/varad/archive/2004/09/13/229182.aspx) also check this out, as it might answer your second question, with solutions. http://stackoverflow.com/questions/2314496/c-sharp-list-member-needs-deep-copy – Paul Zaczkowski Nov 02 '11 at 08:29
  • all you did was change a reference, this is fundamental to almost any programming language, you need to realize there is a distinction between the reference name and the creation of a new instance, in your example you simple changed both references to point to the same instance, your original second list was never subsequently updated and most likely got garbage collected – meandmycode Nov 02 '11 at 08:31
  • Also see this: http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx – Paul Zaczkowski Nov 02 '11 at 08:32
  • Instead of a foreach loop, you can use `listOfJobs2.AddRange(listOfJobs1);` – tafa Nov 02 '11 at 08:32
  • Thanks Tafa! Dunno why I didn't think of that, thanks for the links Paul. –  Nov 02 '11 at 08:33

1 Answers1

5

listOfJobs1 and listOfJobs2 are both references to an object (ie they point to where the object is stored). When you write:

listOfJobs2 = listOfJobs1;

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.

Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
  • Thanks a lot for explaining that simply mate, I grasp it now, I'm assuming this is something I really should've known about anyway to be honest. I'll mark as accepted as soon as I can. –  Nov 02 '11 at 08:34