1

Here is my code c#

 List<Element> TMPElementsX = new List<Element>();
 TMPElementsX = TMPElements;
 ResultsElements.Add(TMPElementsX);
 Element ee = element;
 TMPElements.Remove(ee);

When I removed ee, it leads to removed it also from other collection {TMPElementsX },How can I keep a copy of ee in the TMPElementsX . I need to keep a copy from ee object inside another temporary collection which is TMPElementsX

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Mike Bluer
  • 63
  • 6
  • You have only one list in memory. Create two list List TMPElementsX = new List(); List TMPElements = new List(); – CharithJ Jul 10 '15 at 04:02
  • `TMPElementsX = TMPElements;` after this, both objects will point to the same list. Try to write a copy function just copy the value of the list and create two objects instead of using `=` operator. – Darian Jul 10 '15 at 04:04

2 Answers2

1

The problem is that the objects are pointing to the same memory location, so if you remove it from one of the collections will be removed from the other.

You have to do a deep copy (clone) of the collection.

This post can be useful.

Community
  • 1
  • 1
gastonmancini
  • 1,102
  • 8
  • 19
  • Error 1 The type arguments for method Form1.DeepCopy(System.Collections.ObjectModel.ObservableCollection)' cannot be inferred from the usage. Try specifying the type arguments explicitly. – Mike Bluer Jul 10 '15 at 04:05
  • @MikeBluer Could you give us more context about that error please? Which of the solutions of the post did you try? thanks – gastonmancini Jul 10 '15 at 04:11
  • I tried private static ObservableCollection DeepCopy(ObservableCollection list) where T : ICloneable { ObservableCollection newList = new ObservableCollection(); foreach (T rec in list) { newList.Add((T)rec.Clone()); } return newList; } http://stackoverflow.com/questions/2744750/generic-method-to-create-deep-copy-of-all-elements-in-a-collection – Mike Bluer Jul 10 '15 at 04:13
  • Try to use the solutions from [here](http://stackoverflow.com/questions/14007405/how-create-a-new-deep-copy-clone-of-a-listt) that are easier using linq and without generics. – gastonmancini Jul 10 '15 at 04:24
1

When you execute TMPElementsX = TMPElements;, you actually assign a reference.

It means that both variables TMPElements and TMPElementsX point at the same object. Removing an object using any of these variables will affect this object.

If you want it to be different and independent Lists with the same values then you can clone your List.

If would be much easier if you had a List of value-types (like int, double etc.). It would be this way:

List<int> TMPElementsX = new List<int>();
TMPElementsX = new List<int>(TMPElements); // new list created
ResultsElements.Add(TMPElementsX);
int ee = element;
TMPElements.Remove(ee);

However, in case of reference-types copy constructor creates the new list with the same referenced items. You will need to make a deep copy of a List. You can read some articles at StackOverflow to learn how to make a deep copy:

How do I clone a generic list in C#?
Generic method to create deep copy of all elements in a collection

In some cases, making a deep copy can be a bad choice. It's kind of hack but not solution. Probably, you can change your architecture and logics to work without it.

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101