3

I need to store two variables and then check if they have not changed.

List<CatalogInfo> list_catalogs = new List<CatalogInfo>();
List<FileInfo> list_files = new List<FileInfo>(); 
List<CatalogInfo> list_catalogs_for_check_changed = new List<CatalogInfo>();
List<FileInfo> list_files_check_changed = new List<FileInfo>();

When I do:

list_catalogs_for_check_changed = list_catalogs;
list_files_check_changed = list_files;

But When I add to list_catalogs or list_files Items I see in debager that Items add to list_catalogs_for_check_changed or list_files_check_changed. Why??? I don't add Items to with variables.

  list_catalogs.Add(new CatalogInfo() { Action = "Create", Path = folderBrowserDialog1.SelectedPath });
Ant P
  • 24,820
  • 5
  • 68
  • 105
zuh4n
  • 448
  • 1
  • 4
  • 11
  • 2
    You need to understand the difference betweeen Value types and Reference types. http://www.albahari.com/valuevsreftypes.aspx – Sriram Sakthivel Oct 28 '13 at 08:20
  • possible duplicate of [Deep cloning objects in C#](http://stackoverflow.com/questions/78536/deep-cloning-objects-in-c-sharp) – H H Oct 28 '13 at 08:31
  • @HenkHolterman That question has a similar answer but is not at all the same question. – Ant P Oct 28 '13 at 08:33
  • It is close enough and since this question has a totally nondescript title it's not worth keeping open. – H H Oct 28 '13 at 08:40
  • @HenkHolterman I disagree - "why doesn't this work" and "how do I achieve this" are completely different; plus, there is no need to implement any custom cloning just to copy a list, so the answers to the other question are completely inappropriate. If the title is "nondescript," maybe edit it? Closing a question as a duplicate because of a bad title is a total non-sequitur. – Ant P Oct 28 '13 at 08:47

3 Answers3

6

When you do this:

list_catalogs_for_check_changed = list_catalogs;

You are not making a copy of the list, you are assigning a new reference to the same list. If you want to create a new list with the same items, do this:

    list_catalogs_for_check_changed = new List<CatalogInfo>(list_catalogs);

This assigns a new List<CatalogInfo> and passes the list from which to copy the elements, resulting in two independent lists with the same items.

Ant P
  • 24,820
  • 5
  • 68
  • 105
3

I don't add Items to with variables.

Indeed, you don't. You are adding items to the lists. If you do (from the question):

list_catalogs_for_check_changed = list_catalogs;
list_files_check_changed = list_files;

Then both list_catalogs_for_check_changed and list_catalogs hold a reference to the same list of CatalogInfo. Likewise, list_files and list_files_check_changed hold a reference to the same list of FileInfos. It therefore follows that if you add an item to that list, it will be visible via either variable.

The variable is not the list: the list is somewhere on the managed heap. The variable is just the reference to the list. Assigning one list variable to another copies the reference. It does not make a copy of the list.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

When you do

list_catalogs_for_check_changed = list_catalogs;

You are handing over a reference to list_catalogs. You want to copy it.

This is an article describing value types vs reference

germi
  • 4,628
  • 1
  • 21
  • 38